[Last Call] Learn how to a build a cloud-first strategyRegister Now


How to deal with CrtIsValidHeapPointer() error?

Posted on 2002-04-19
Medium Priority
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

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 800 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?


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


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.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

829 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