Solved

How to deal with CrtIsValidHeapPointer() error?

Posted on 2002-04-19
10
1,904 Views
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.

Thanks,
Frank
0
Comment
Question by:magenta
10 Comments
 
LVL 3

Expert Comment

by:job_s
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 .


jobs
0
 
LVL 86

Accepted Solution

by:
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.
0
 

Author Comment

by:magenta
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?

Thanks,
Frank
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:magenta
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 }
  }

0
 
LVL 86

Expert Comment

by:jkr
ID: 6957133
What exactly is

// do something with i

?
0
 

Author Comment

by:magenta
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.

Thanks,
Frank
0
 
LVL 86

Expert Comment

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

Author Comment

by:magenta
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...

Thanks,
Frank
0
 
LVL 86

Expert Comment

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

Expert Comment

by:Moondancer
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.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
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.
http://www.experts-exchange.com/questions/Q.20115902.html
http://www.experts-exchange.com/questions/Q.20123776.html
http://www.experts-exchange.com/questions/Q.20184557.html
http://www.experts-exchange.com/questions/Q.20192504.html
http://www.experts-exchange.com/questions/Q.20245395.html
http://www.experts-exchange.com/questions/Q.20254214.html
http://www.experts-exchange.com/questions/Q.20271357.html
http://www.experts-exchange.com/questions/Q.20279551.html
http://www.experts-exchange.com/questions/Q.20286253.html
http://www.experts-exchange.com/questions/Q.20291661.html
http://www.experts-exchange.com/questions/Q.20294731.html

To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.20286299.html

*****  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
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed in the link below
http://www.experts-exchange.com/commspt/Q.20277028.html
 
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.
Moondancer
Moderator @ Experts Exchange
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

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: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

770 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