CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2005
    Posts
    159

    Debug assertion failed: _CrtIsValidHeapPointer

    Hi all,

    My main application is calling a method of class which is implemented in a dll. I have the possibility to change both and after a bit of googling I was able to sort out my problem. It appears that when I pass a std::wstring by value to the dll I get the assertion, but when I pass it by reference everything works fine. I get the assertion at the moment the dll method returns.

    I would like to understand why this is caused.

    My understanding is that when I pass a string by value the copy constructor is called to create a local instance in the application heap. When the method returns it tries to delete this local instance but it is not in the dll heap and as such this fails with the assertion.

    Can somebody confirm this or explain why this is happening?

    thank you,

    Jef

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Debug assertion failed: _CrtIsValidHeapPointer

    It is dangerous to potentially allow heap allocations done in a DLL to be released outside the DLL, or vice versa.

    In fact, it's a bit iffy to even pass a C++ class like std::wstring over a DLL boundary by reference----what if the DLL was built against a different version of the STL, and std::wstring had a different structure there?

    In general, you should limit the data passing over a DLL boundary to primitive types.

  3. #3
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Debug assertion failed: _CrtIsValidHeapPointer

    In addition to Lindley's post, passing by reference may also create problems:
    http://support.microsoft.com/kb/172396

    There are a lot of articles about this problem. Avoid using STL classes in Dll interface. Use plain pointers instead of std::wstring.

  4. #4
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Debug assertion failed: _CrtIsValidHeapPointer

    That is probably DLL and EXE are using different versions of runtime library. Or one is using Debug build or another is using Retail/Release build. Make both of them consistent. Use Dependency Walker to find out what Runtime DLLs are being used.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured