Click here to Skip to main content
15,914,013 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: How to a get a coloured button Pin
Bob Stanneveld16-Dec-03 21:03
Bob Stanneveld16-Dec-03 21:03 
AnswerRe: How to a get a coloured button Pin
satadru16-Dec-03 21:14
satadru16-Dec-03 21:14 
General'string' to 'enum' conversion. Pin
WREY16-Dec-03 10:49
WREY16-Dec-03 10:49 
GeneralRe: 'string' to 'enum' conversion. Pin
BaldwinMartin16-Dec-03 11:16
BaldwinMartin16-Dec-03 11:16 
GeneralRe: 'string' to 'enum' conversion. Pin
Michael Dunn16-Dec-03 11:43
sitebuilderMichael Dunn16-Dec-03 11:43 
GeneralRe: 'string' to 'enum' conversion. Pin
WREY16-Dec-03 12:54
WREY16-Dec-03 12:54 
GeneralRe: 'string' to 'enum' conversion. Pin
Robert A. T. Káldy18-Dec-03 3:20
Robert A. T. Káldy18-Dec-03 3:20 
GeneralHeap Hell Pin
Matthew Busche16-Dec-03 10:06
Matthew Busche16-Dec-03 10:06 
I have spent the last 24 hours tracking down a heap corruption problem. I think I now know vaguely what the problem is, but it has made it clear I have no idea what I'm doing when it comes to heap usage with DLLs under windows. I am hoping some seasoned windows programmer out there can first look at my sad story below, and then either educate me as to the do's and don'ts of heap usage with DLLs under windows or point me to an article or book that will educate me. I'm not looking to be an expert, but I don't want to hit any more snafus like this one.

MY SAD STORY:

I'm working on my first windows DLL. I have an exported function that looks much like this one:

void __declspec(dllexport) getCommandString(std::string& commandString);

In case it's not obvious, usage of this function is for the application to pass in a string by reference, then some code in the DLL sets it for you.

Perhaps you already know my problem. If you do, YOU are the person I hope to hear from. But I'll finish my sad story for those of you who don't.

So I had some application code that called this function, much like this:

void doSomething()
{
    std::string commandString;
    getCommandString(commandString);
    // do some stuff with the string
    return;
}

doSomething() fails at the return statement with an assertion failure when memory for commandString was being freed. Digging into the stack trace I found the assertion failure was occuring somewhere in the implementation of this system call:

HeapValidate( _crtheap, 0, pHdr(pUserData) );

I read the docs for HeapValidate and discovered you could set the thrid arg to NULL to force the entire heap to be validated. So I immediately had the idea of sprinkling my own calls to HeapValidate throughout my code to track down the time when I was corrupting the heap. But then I couldn't figure out where to get the symbol _crtheap. I could see it in the debugger, but couldn't find it declared in any headers that came with VC6. So I says to myself, "I'll just hard code the address since surely _crtheap won't change values from run to run." But this caused me problems too. Eventually I figured out that that value of the global symbol _crtheap was changing within a single run! After some additional contemplation, I says to myself, "Perhaps there is more than one heap for my process!" Through additional experimentation, I now believe that my DLL is allocating and freeing memory from one heap, and my application is using another. The above functions cause memory to be allocated in the context of the DLL and freed in the context of the application (and vice-versa).

Voila! Crash!

MY QUESTIONS:

I would like someone to confirm my theorys and perhaps provide me any additional information as to the usage of heaps in DLLs. Does every DLL have its own heap? I currently believe that I must eliminate any interfaces where memory is allocated in one library context and freed in another. What if I changed the signature of my fucntion to:

std::string __declspec(dllexport) getCommandString();

Would this eliminate my problem?

Thanks much,
Matt
GeneralRe: Heap Hell Pin
Michael Dunn16-Dec-03 10:13
sitebuilderMichael Dunn16-Dec-03 10:13 
GeneralRe: Heap Hell Pin
forjer16-Dec-03 11:11
forjer16-Dec-03 11:11 
GeneralEMail Logic Question Pin
BaldwinMartin16-Dec-03 9:11
BaldwinMartin16-Dec-03 9:11 
GeneralRe: EMail Logic Question Pin
Alexander M.,16-Dec-03 9:17
Alexander M.,16-Dec-03 9:17 
GeneralRe: EMail Logic Question Pin
BaldwinMartin16-Dec-03 9:23
BaldwinMartin16-Dec-03 9:23 
GeneralRe: EMail Logic Question Pin
Garth J Lancaster16-Dec-03 10:21
professionalGarth J Lancaster16-Dec-03 10:21 
GeneralRe: EMail Logic Question Pin
BaldwinMartin16-Dec-03 10:35
BaldwinMartin16-Dec-03 10:35 
GeneralRe: EMail Logic Question Pin
Garth J Lancaster16-Dec-03 11:02
professionalGarth J Lancaster16-Dec-03 11:02 
GeneralRe: EMail Logic Question Pin
BaldwinMartin16-Dec-03 11:14
BaldwinMartin16-Dec-03 11:14 
GeneralRe: EMail Logic Question (other option, the best) Pin
Tim Smith16-Dec-03 12:08
Tim Smith16-Dec-03 12:08 
GeneralRe: EMail Logic Question Pin
Ravi Bhavnani16-Dec-03 18:34
professionalRavi Bhavnani16-Dec-03 18:34 
GeneralSys Info Pin
Chechi16-Dec-03 8:17
Chechi16-Dec-03 8:17 
GeneralRe: Sys Info Pin
Alexander M.,16-Dec-03 8:53
Alexander M.,16-Dec-03 8:53 
GeneralRe: Sys Info Pin
BaldwinMartin16-Dec-03 10:57
BaldwinMartin16-Dec-03 10:57 
GeneralRe: Sys Info Pin
Tom Wright16-Dec-03 17:59
Tom Wright16-Dec-03 17:59 
GeneralRe: Sys Info Pin
BaldwinMartin16-Dec-03 22:57
BaldwinMartin16-Dec-03 22:57 
GeneralRe: Sys Info Pin
Bob Stanneveld16-Dec-03 21:22
Bob Stanneveld16-Dec-03 21:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.