473,386 Members | 1,841 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,386 software developers and data experts.

_CrtIsValidHeapPointer

Hello,

My application is using a Dll loaded with the function
"LoadLibrary(<NameOfTheDll>)"

In the dll there is the following fonction :

char * GetMessageError(int Error)
{

char * pMessageError;

CString ErrorMessage;

(...)// filling of Error message

pMessageError= new char[ErrorMessage.GetLength()+1];
strcpy_s(pMessageError,ErrorMessage.GetLength()+1, ErrorMessage.GetBuffer());

return pMessageError;
}

In my application there is the function :
void DisplayMessageError(int DllError)
{
char * pMessageError= GetMessageError(DllError);

if(pMessageError){

AfxMessageBox(pMessageError);
delete pMessageError;//!!!!!!!!!!!---------->Problem at this line
}
}
In debug mode when executing the line 'delete pMessageError'
I have the following assertion

/*
* 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.
*/
_ASSERTE(_CrtIsValidHeapPointer(pUserData));

The Dll seems to have its own heap.

How can I solve this problem ?

JsCHarly
May 24 '07 #1
3 1992
Hi,
My application is using a Dll loaded with the function
"LoadLibrary(<NameOfTheDll>)"

In the dll there is the following fonction :

char * GetMessageError(int Error)
{
char * pMessageError;
CString ErrorMessage;
(...)// filling of Error message
pMessageError= new char[ErrorMessage.GetLength()+1];
This is allocated in the heap of the current c++ runtime
strcpy_s(pMessageError,ErrorMessage.GetLength()+1, ErrorMessage.GetBuffer());

return pMessageError;
}

In my application there is the function :

void DisplayMessageError(int DllError)
{
char * pMessageError= GetMessageError(DllError);

if(pMessageError){

AfxMessageBox(pMessageError);
delete pMessageError;//!!!!!!!!!!!---------->Problem at this line
Two errors:
1) new [] must be complemented with delete []. So it should read "delete []
pMessageError;"

2) it is freed in this modules c++ runtime heap which might be different,
except when both are compiled against the same dll runtime version.
Thats what the error is telling you: wrong heap.

There are two common ways to overcome that problem:
a) let the caller allocate the memory and pass a pointer to that memory plus
the size of the buffer to the callee

void GetMessageError(int Error, char* buf, size_t bufSize)

char* p = new char[100];
GetMessageError(42, p, 100);
// use p
delete [] p;

b) Export a delete function in your dll which returns heap memory to its
callers

void FreeMemory(char *p); // implemented in the called dll

char* p = GetMessageError(42);
// use p
FreeMemory(p);

You might need different free functions for different types of allocated
memory. E.g. to differentiate between new/delete and new[]/delete[]

--
SvenC

May 24 '07 #2
Thank you for your answer which solved my problem.

I would like to better understand one thing of your reply.

You wrote :
2) it is freed in this modules c++ runtime heap which might be different,
except when both are compiled against the same dll runtime version.
What do you mean by " except when both are compiled against the same dll
runtime version"

JsCHarly
"SvenC" wrote:
Hi,
My application is using a Dll loaded with the function
"LoadLibrary(<NameOfTheDll>)"

In the dll there is the following fonction :

char * GetMessageError(int Error)
{
char * pMessageError;
CString ErrorMessage;
(...)// filling of Error message
pMessageError= new char[ErrorMessage.GetLength()+1];

This is allocated in the heap of the current c++ runtime
strcpy_s(pMessageError,ErrorMessage.GetLength()+1, ErrorMessage.GetBuffer());

return pMessageError;
}

In my application there is the function :

void DisplayMessageError(int DllError)
{
char * pMessageError= GetMessageError(DllError);

if(pMessageError){

AfxMessageBox(pMessageError);
delete pMessageError;//!!!!!!!!!!!---------->Problem at this line

Two errors:
1) new [] must be complemented with delete []. So it should read "delete []
pMessageError;"

2) it is freed in this modules c++ runtime heap which might be different,
except when both are compiled against the same dll runtime version.
Thats what the error is telling you: wrong heap.

There are two common ways to overcome that problem:
a) let the caller allocate the memory and pass a pointer to that memory plus
the size of the buffer to the callee

void GetMessageError(int Error, char* buf, size_t bufSize)

char* p = new char[100];
GetMessageError(42, p, 100);
// use p
delete [] p;

b) Export a delete function in your dll which returns heap memory to its
callers

void FreeMemory(char *p); // implemented in the called dll

char* p = GetMessageError(42);
// use p
FreeMemory(p);

You might need different free functions for different types of allocated
memory. E.g. to differentiate between new/delete and new[]/delete[]

--
SvenC
May 24 '07 #3
Hi,
I would like to better understand one thing of your reply.

You wrote :
>2) it is freed in this modules c++ runtime heap which might be
different, except when both are compiled against the same dll
runtime version.

What do you mean by " except when both are compiled against the same
dll runtime version"
In the project settings you can choose to statically link to the crt which
will always give that module its own heap.
You can also choose to use the dll crt which exists as release and debug
version and is different for all VC++ compilers (6, 7.0, 7.1, 8.0, ...) So
any mixture of release/debug and/or compiler versions will also introduce
separate heaps.

--
SvenC

May 24 '07 #4

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

Similar topics

0
by: vikki | last post by:
I have an application which, in response to a button click, opens a secondary window to show a report. The window simply consists of a subclass of CFrameWnd and a subclass of CHtmlView. ...
4
by: Pierre Couderc | last post by:
I have a problem with the declaration of virtual function. What do I do wrong? Why the compiler (MS VC++ 6) says nothing? It is very simple (see my comment below): class A { public : A(){;}...
4
by: Adriano Coser | last post by:
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. ...
0
by: jiing | last post by:
When I execute my program (it's multithread and has COM and dll), there is an error message as follows: Program D:\dongle_1\TestAP\bin\Debug\TestAP.exe File: dbgheap.c Line: 1132 Expression:...
0
by: jiing | last post by:
When I execute my program (it's multithread and has COM and dll), there is an error message as follows: Program D:\dongle_1\TestAP\bin\Debug\TestAP.exe File: dbgheap.c Line: 1132 Expression:...
2
by: ratherthanwords | last post by:
Hi... So I'm writing some code that does some matrix operations (namely multiplications) I've created a separate class for manipulating the matrices. The matrix itself has memory for it...
13
by: Schizoid Man | last post by:
Hi, I have defined a very simple class as follows. I initialize the CInventory object as: CInventory *itemInveotry; The program just performs two functions - it reads the total number of...
3
by: Maxim | last post by:
Hi all, I'm trying to use ATL (CString, CCOMSafeArray, etc.) classes in managed C++ code, but I get the following assert exeption when I start the application: Debug Assertion Failed: ...
6
by: =?iso-8859-1?q?Erik_Wikstr=F6m?= | last post by:
I have a native DLL which is called from a managed application. One of the calls to the DLL returns a std::vector<Cell(by value) where Cell is a POD type, everything works fine until the function...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.