_CrtIsValidHeapPointer exception

A

Adriano Coser

I'm having a _CrtIsValidHeapPointer assertion at the _free_dbg_lk
function. I'm calling a function from a DLL witch allocates (malloc) a
char array and the exception occours freeing the array.

The DLL that causes the problem is compiled and linked on the same way
that the others that form my application. All involved DLLs use
'Multi-threaded Debug DLL' runtime library.

If I include the CPP that causes the problem directly on the app
project, the error disapears.

Does anyone have an idea of what could be the problem? I'm using VS
2003.

Thanks in advance for any help.

Adriano.
 
J

Jochen Kalmbach

Hi Adriano!
I'm having a _CrtIsValidHeapPointer assertion at the _free_dbg_lk
function. I'm calling a function from a DLL witch allocates (malloc) a
char array and the exception occours freeing the array.

Are you sure that the memory was allocasted *in* the DLL?
Or that you use in all DLLs/EXEs the DLL-Version of the CRT?
The DLL that causes the problem is compiled and linked on the same way
that the others that form my application. All involved DLLs use
'Multi-threaded Debug DLL' runtime library.

If I include the CPP that causes the problem directly on the app
project, the error disapears.

Then the settings are not the same in all projects...

I strongly encourage you that you free the memory in the DLL/EXE which
has allocated the memory!

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
A

Adriano Coser

Thanks for your answer Jochen.

In fact the problem occours with a string class that I implemented. It
has an internal char* buffer that's allocated/deallocated with malloc
and free.

This class is exported by a DLL. Instances of the class are passed by
value as parameters for so many functions accross my application (exe
and dlls), and it has never give me a problem.

Now I have a new DLL and when I call a function from it passing my
string object as a paremeter I have the exception when the automatic
destructor is called for my string.

I quite sure my DLLs are compiled the same way, but I'll take another
look.

Thanks,
Adriano.
 
T

Tom Widmer

Adriano said:
Thanks for your answer Jochen.

In fact the problem occours with a string class that I implemented. It
has an internal char* buffer that's allocated/deallocated with malloc
and free.

This class is exported by a DLL. Instances of the class are passed by
value as parameters for so many functions accross my application (exe
and dlls), and it has never give me a problem.

Now I have a new DLL and when I call a function from it passing my
string object as a paremeter I have the exception when the automatic
destructor is called for my string.

I quite sure my DLLs are compiled the same way, but I'll take another
look.

Well, it's always dangerous implementing your own versions of things
like a string class, since it's easy enough to leave in the odd bug. I
take it you've got the fundamentals right, like the copy constructor and
copy assignment operator? If so, and all your code is compiled against
the exact same CRT DLL lib, then you should be ok. Otherwise, it's
probably down to a bug in your string class, or elsewhere in your
application.

Tom
 
J

john_1726

Tom said:
Tom, could you please tell me, how could an incorrectly implemented
copy constructor or assignment operator cause a CrtIsValidHeapPointer
error? Would it simply be because the copy constructor or assignment
operator may be allocating memory but trying to free that memory across
assembly boundaries?

Thank you,

Roger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top