The following code is executed without any error in debug mode of Visual C++ 2017 (15.2).
#include <iostream>
int main()
{
int* ptr1 = new int( 2 );
int* ptr2 = ptr1;
delete ptr2;
ptr2 = nullptr;
int res = 40 + (*ptr1); // invalid
std::cout << res << std::endl;
}
But I would expect the debugger to throw an error when the invalid pointer is dereferenced. Is there an option to activate the needed checks?
Back story: I just found a bug in some old legacy code (hence the raw pointers) that essentially boils down to the problem shown above. And the search would have taken muss less time if the Debugger could track the validity of pointers. Basic Runtime Checks (/RTC1
) and Security Check (/GS
) are enabled. Static code analysis did not warn about this problem is the original code, which is a lot more complicated than this distilled minimal example.