Solved

How to deal with CrtIsValidHeapPointer() error?

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

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now