[VS2003] operator delete[] causes exception at runtime

P

Pascalus

Hi there!
I have a problem with the delete[] operator to destroy a (char*). It used to work since today. I don't know what I may
have changed in the project and/or the solution (probably in the properties or what) to cause this problem...

I have an app written in cpp.NET. In one function, I call an external function from a win32 DLL of my own. Here is the body:

#include "myDll.h"
void myCppNetFunction(...)
{
char* str = NULL;
...
myDllFunction(&str); // declared as "__declspec(dllimport) void myDllFunction(char** str);" in the header file
...
delete[] str;
}

myDllFunction() takes the address of the pointer and allocates memory for the pointer. The calling function must then
destroy the pointer after usage.
__declspec(dllexport) void myDllFunction(char** str)
{
*str = new char[...];
...
}

There is no problem when compiling and linking.
At runtime, I got an exception on the delete[] instruction, generated by a:
_ASSERTE(_CrtIsValidHeapPointer(pUserData));
in a function called:
_CRTIMP void __cdecl _free_dbg()
in a file called:
dbgheap.c

Well, I'm not an expert at all in Managed C++ and the VS2003 environment. Is my code good or wrong?
Your help will be much appreciated !!!

Thx,
/Pascal.
 
C

Carl Daniel [VC++ MVP]

Pascalus said:
Hi there!
I have a problem with the delete[] operator to destroy a (char*). It
used to work since today. I don't know what I may have changed in the
project and/or the solution (probably in the
properties or what) to cause this problem...
I have an app written in cpp.NET. In one function, I call an external
function from a win32 DLL of my own. Here is the body:
#include "myDll.h"
void myCppNetFunction(...)
{
char* str = NULL;
...
myDllFunction(&str); // declared as "__declspec(dllimport) void
myDllFunction(char** str);" in the header file ...
delete[] str;
}

myDllFunction() takes the address of the pointer and allocates memory
for the pointer. The calling function must then destroy the pointer
after usage. __declspec(dllexport) void myDllFunction(char** str)
{
*str = new char[...];
...
}

There is no problem when compiling and linking.
At runtime, I got an exception on the delete[] instruction, generated
by a: _ASSERTE(_CrtIsValidHeapPointer(pUserData));
in a function called:
_CRTIMP void __cdecl _free_dbg()
in a file called:
dbgheap.c

Well, I'm not an expert at all in Managed C++ and the VS2003
environment. Is my code good or wrong? Your help will be much appreciated
!!!

There's no managed C++ involved - as far as I can tell.

Your problem is that you're allocating in a DLL and releasing in an EXE (or
another DLL), and those two modules are linked to different copies of the
C++ runtime library.

For this kind of architecture to work, you must link everything against a
single DLL version of the runtime library so that you have a single heap
serving the entire application.

If you can't control which runtime library your DLLs client (i.e. the EXE)
is linked with, then you need to export a delete function from your DLL and
have your clients call that function instead of delete[].

-cd
 

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