Click here to Skip to main content
15,925,042 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Generaldifferent dll - must use SysAllocString and SysFreeString? Pin
devvvy23-Aug-07 14:01
devvvy23-Aug-07 14:01 
GeneralRe: different dll - must use SysAllocString and SysFreeString? Pin
jhwurmbach23-Aug-07 21:37
jhwurmbach23-Aug-07 21:37 
AnswerRe: heap corruption Pin
Iain Clarke, Warrior Programmer23-Aug-07 1:33
Iain Clarke, Warrior Programmer23-Aug-07 1:33 
GeneralRe: heap corruption Pin
Russell'23-Aug-07 1:49
Russell'23-Aug-07 1:49 
GeneralRe: heap corruption Pin
devvvy23-Aug-07 13:51
devvvy23-Aug-07 13:51 
GeneralRe: heap corruption Pin
Iain Clarke, Warrior Programmer23-Aug-07 21:54
Iain Clarke, Warrior Programmer23-Aug-07 21:54 
GeneralRe: heap corruption Pin
devvvy29-Aug-07 2:58
devvvy29-Aug-07 2:58 
AnswerRe: heap corruption Pin
ghle27-Aug-07 4:24
ghle27-Aug-07 4:24 
You are missing the point, devvvy. You have two problems:
a) the new/delete pairing, which you said you've fixed (original post was new/free pairing which is not correct).
b) the actual location of the new/delete pairing - this is causing the CRASH.

devvvy wrote:

I tried:
(a) "new" with "delete"
and
(b) "free" with "malloc"
Still crashing the very same way... and besides I think I'm already doing this.


You are pairing new with free(), not with delete, so you need to make the change.

However, you need to use the correct pairing (new & delete) in the same routine.

Your logic:
Create a new buffer in main();
Free buffer in main(); // Works okay
Create a new buffer in the DLL;
Free that buffer outside of the DLL, in the main program; // Crashes

You can't do that because the main program and the DLL have different heaps. The main program does not have access to the DLL's heap, so the pointer is invalid upon return to main.

Either create the 2nd buffer in main and also pass the 2nd buffer to NullTerminateString, or add a new method to the CUtilities class that deletes pszResult when you are done with it.

Reread the comments on the _CrtIsValidHeapPointer() before the _ASSERTE(). That describes your problem exactly.

devvvy wrote:
/*
* If this ASSERT fails, a bad pointer has been passed in. It may be
* totally bogus, or it may have been allocated from another heap.
* The pointer MUST come from the 'local' heap.
*/


<br />
It is difficult to tell the intended purpose of NullTerminateString().  The logic (use of string.size()) and variable names (pszData) both indicate the input is already a null-terminated string, but my brain says that you actually want to add a null to a non-null-terminated string.  Not clear...<br />
<br />
I assume the purpose of NullTerminateString() is therefore to add a second null to the end of an already null-terminated string, because the routine fails if char * pszData is not already null terminated. (It can't determine proper size at string.size().)<br />
<br />
<br />
<blockquote class="FQ"><div class="FQA">devvvy wrote:</div>ch = pszBuffer[5]; // Should be '\0'<br />
free(pszBuffer); << LOCATION2: CRASH Here!<br />
</blockquote><br />
<br />
Note: pszBuffer[5] WILL BE NULL even without calling NullTerminateString()!<br />
Set szBuffer1 to 'a','b','c','d','e','f','g','h','i','j' and watch things go crazy.<br />


Gary
GeneralRe: heap corruption Pin
devvvy29-Aug-07 2:57
devvvy29-Aug-07 2:57 
QuestionSmall question Pin
Chen-XuNuo22-Aug-07 23:01
Chen-XuNuo22-Aug-07 23:01 
AnswerRe: Small question Pin
fefe.wyx22-Aug-07 23:12
fefe.wyx22-Aug-07 23:12 
AnswerRe: Small question Pin
toxcct22-Aug-07 23:17
toxcct22-Aug-07 23:17 
AnswerRe: Small question Pin
Roger Broomfield22-Aug-07 23:20
Roger Broomfield22-Aug-07 23:20 
JokeRe: Small question Pin
Cedric Moonen22-Aug-07 23:22
Cedric Moonen22-Aug-07 23:22 
GeneralRe: Small question Pin
toxcct22-Aug-07 23:25
toxcct22-Aug-07 23:25 
GeneralRe: Small question Pin
Roger Broomfield22-Aug-07 23:32
Roger Broomfield22-Aug-07 23:32 
GeneralRe: Small question Pin
Naveen22-Aug-07 23:33
Naveen22-Aug-07 23:33 
GeneralRe: Small question Pin
Naveen22-Aug-07 23:30
Naveen22-Aug-07 23:30 
Questionregistry cleaner for window xp Pin
niki dutta22-Aug-07 22:57
niki dutta22-Aug-07 22:57 
AnswerRe: registry cleaner for window xp Pin
toxcct22-Aug-07 22:58
toxcct22-Aug-07 22:58 
AnswerRe: registry cleaner for window xp Pin
Russell'22-Aug-07 23:05
Russell'22-Aug-07 23:05 
AnswerRe: registry cleaner for window xp Pin
Rajesh R Subramanian23-Aug-07 0:21
professionalRajesh R Subramanian23-Aug-07 0:21 
GeneralRe: registry cleaner for window xp Pin
toxcct23-Aug-07 0:26
toxcct23-Aug-07 0:26 
GeneralRe: registry cleaner for window xp Pin
Iain Clarke, Warrior Programmer23-Aug-07 1:35
Iain Clarke, Warrior Programmer23-Aug-07 1:35 
QuestionHow to get software information Pin
kim00722-Aug-07 22:41
kim00722-Aug-07 22:41 

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.