473,378 Members | 1,623 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

[VS2003] operator delete[] causes exception at runtime

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.
Jun 7 '06 #1
1 1670
Pascalus wrote:
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
Jun 7 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Senthilvel Samatharman | last post by:
I am just curious about the case 3 in the follwouing program. I understand that case 1 is the right way to overload, while Case 2 is erroneous. But i do think that the implementation of operator...
10
by: Ramprasad A Padmanabhan | last post by:
Hi All, I am a c/perl programmer trying my hand at C++. In my code below I have an array class where I am trying to add two arrays using "+" . I am not sure why I get a '0' always for the first...
5
by: Niall | last post by:
I have an unmanaged C++ ray tracer which I am working to put a C# front end on. It runs fine as just the unmanaged code. I have made a MC++ wrapper DLL to expose the required types to the C#...
7
by: YAZ | last post by:
Hello, I have a dll which do some number crunching. Performances (execution speed) are very important in my application. I use VC6 to compile the DLL. A friend of mine told me that in Visual...
16
by: Joseph Paterson | last post by:
Hello, I've created a class to store 2 dimensional matrices, and I've been trying to overload the operator, so access to the elements of the matrix is easy. I have 3 private variables, _m, _row...
0
by: erik.erikson | last post by:
I am getting a compiler error that I can't well explain or even understand the origin of (though I boiled it down close...). Below is a bare-bones example. What I am doing is defining the...
12
by: Premal | last post by:
Hi, I tried to make delete operator private for my class. Strangely it is giving me error if I compile that code in VC++.NET. But it compiles successfully on VC++6.o. Can anybody give me inputs...
3
by: C++Liliput | last post by:
Hi, I was looking at the implementation of operator new and operator new in gcc source code and found that the implementation is exactly the same. The only difference is that the size_t argument...
8
by: Rahul | last post by:
Please read the following code class Test{ public: void * operator new (size_t t) { return malloc(t); } void operator delete (void *p) { free(p); } };
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.