I have enabled Address Sanitizer for my project in Visual Studio and successfully tested it on the following code from Microsoft Learn.
#include <stdio.h>
int x[100];
int main() {
printf("Hello!\n");
x[100] = 5; // Boom!
return 0;
}
However, the sanitizer can't find the missing delete statement in the following code:
struct Object {
int x;
int y;
};
int main() {
Object* obj = new Object();
// Boom!
return 0;
}
Looking at the resulting assembly we can see that the new operator is indeed called and isn't optimized away. The following output is taken from Debug/x86 configuration but similar outputs can be obtained for configurations Debug/x64, Release/x86 and Release/x64.
; 6 : int main() {
push ebp
mov ebp, esp
sub esp, 12 ; 0000000cH
mov ecx, OFFSET __62A33F1D_Source@cpp
call @__CheckForDebuggerJustMyCode@4
; 7 : Object* obj = new Object();
push 8
call ??2@YAPAXI@Z ; operator new
Can Address Sanitizer detect this type of error? If yes, how can I achieve a successful error detection?
CodePudding user response:
Microsoft address sanitizer does not detect memory leaks. See the second note on the linked page.
... Send us feedback on what you'd like to see in future releases. Your feedback helps us prioritize other sanitizers for the future, such as
/fsanitize=thread
,/fsanitize=leak
,/fsanitize=memory
, ...