Search code examples
visual-studiovisual-studio-2008memory-leaksmfcwindows-vista

"Vista-only" heap corruption in a MFC application


Ours is a MFC application, which links to a win32 DLL. When the application invokes this DLL function, the argument "buffer" becomes a "Bad Pointer", after entering the function stack. This results in an application crash.

static MyClass* Instance(string& buffer);

I changed the argument type to "char *", but it only pushes the crash to next statement in the function. How to detect this heap corruption?

Few hints

  • This crash is reproducible, even if I invoke this DLL function from the very start of our application (CWinApp constructor). Could this memory corruption be caused by loading of resources, manifest etc?
  • The crash is ocurring in Vista and Win7, but not in XP.
  • Both these projects were recently migrated from Visual Studio 2002 to VS2008.

Code that invokes the function

CString data = "some string";
string str = data.GetBuffer();
data.ReleaseBuffer();
MyClass *obj = MyClass::Instance(str);

Solution

  • There were two mistakes:

    1. Couple of custom built C++ files were not compiled with MD switch. We had to add -MD to the custom build script to make the CRT consistant with other objects.

    2. There were LNK2005 conflicts between LIBCMT.LIB and MSVCRT.LIB, which were otherwise ignored due to the /FORCE switch. We resolved these conflicts by removing LIBCMT.LIB in Linker->Input

    Thanks all for your help.