Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / May 2007

Tip: Looking for answers? Try searching our database.

_CrtIsValidHeapPointer

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JsCharly - 24 May 2007 08:01 GMT
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
SvenC - 24 May 2007 08:48 GMT
Hi,
> My application is using a Dll loaded with the function
> "LoadLibrary(<NameOfTheDll>)"
[quoted text clipped - 7 lines]
> (...)// 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());
>
[quoted text clipped - 11 lines]
> 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
JsCharly - 24 May 2007 10:00 GMT
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

> Hi,
> > My application is using a Dll loaded with the function
[quoted text clipped - 60 lines]
> --
> SvenC
SvenC - 24 May 2007 10:47 GMT
Hi,

> I would like to better understand one thing of your reply.
>
[quoted text clipped - 6 lines]
> 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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.