How to deal with CrtIsValidHeapPointer() error?

Posted on 2002-04-19
Last Modified: 2012-08-14
I have a .exe and that dynamically loads a .dll using LoadLibrary(). Memory is allocated in the .exe and deallocated in the .dll. However, some of the deallocations assert in CrtIsValidHeapPointer() for debug builds.

What exactly am I doing wrong and what can I do about it? I've made sure that the compiler options are the same for both the .exe and the .dll. I've made sure that I'm deallocating correctly. I think it has something to do with the heaps being separate between the .exe and the .dll. I'm really stuck here...please help.

Question by:magenta
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 6955807
If ur using Debug build Exe and release Dll or Release dll and Debug exe. This problem will occur, because allocation is done by one funcation DEBUG_NEW and trying to Delete using release function that's the problem.

Use both release builds or both debug builds .

LVL 86

Accepted Solution

jkr earned 200 total points
ID: 6956472
the problem is that the DLL end the executabl use different allocators, so freeing memory in one module that was allocated in an other module leads to this error. To avoid that, select "Use Runtime Library: Multithreaded DLL" for both the applcation and the DLL - this makes both load the CRT as a DLL, thus they share the same allocators.

Author Comment

ID: 6956938
jkr, your solution removed the CrtIsValidHeapPointer() error, thanks!

However, now I get an access violation! What is strange is that it occurs in an iterator access to a map. The .exe iterates through the map and then invokes a .dll function, and then the .dll iterates through the map---which is when the access violation occurs. The map is unmodified betwen the 2 iterations.

I know you are thinking that I must be doing something bad in my code, but take my word for it, nothing happens between the 2 map iterations except for the .dll invocation. Why would iterating through the map in the .dll cause an access violation? Upon closer inspection, the iterator test "i != map.end()" never returns true.

Any ideas?

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 6956948
Here's some code to illustrate

.exe code:

  map< string, string >::iterator i;
  for ( i = theMap.begin(); i != theMap.end(); i++ )
  { // do something with i }
  DllFunction( map );

.dll code:

  HRESULT DllFunction( map& theMap )
    // this loop executes theMap.size() + 1 times and
    // then the access violation occurs!
    for ( i = theMap.begin(); i != theMap.end(); i++ )
    { // do something with i }

LVL 86

Expert Comment

ID: 6957133
What exactly is

// do something with i


Author Comment

ID: 6957151
I just access what the iterator points to (i.e., i->first, i->second, etc). The important point is that I iterate through the map before I call the .dll function and then again in the .dll function. However, once I iterate through the map from the .dll code, iterating through the map results in an access violation. Note that I do not modify the map or do anything else between the 2 iterations.

LVL 86

Expert Comment

ID: 6959705
What happens if you omit iterating through the map in the app before calling the DLL function?

Author Comment

ID: 6960600
jkr, I didn't try that since it needs to be done. I'll try it out though and let you know.

I'm gonna try to put together a very simple minimal VC++ project that demonstrates this. Hopefully, someone would be willing to try it out...

LVL 86

Expert Comment

ID: 6960609
One other thing: Since you're already trying, try to pass a pointer to the DLL func instead of a reference :o)

Expert Comment

ID: 6979415
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101, Netminder or Mindphaser will return to finalize these if they are still open in 7 days.  Experts, please post closing recommendations before that time.

Below are your open questions as of today.  Questions which have been inactive for 21 days or longer are considered to be abandoned and for those, your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.

To view your locked questions, please click the following link(s) and evaluate the proposed answer.

*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations.
If you are interested in the cleanup effort, please click this link 
POINTS FOR EXPERTS awaiting comments are listed in the link below
Moderators will finalize this question if in @7 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
Thanks everyone.
Moderator @ Experts Exchange

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maze travler 6 72
C++ Language error 28 309
FMX enumerated colours 2 152
thread-safe code in c++ 2 126
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question