Solved

Strange asertation

Posted on 2001-09-04
5
271 Views
Last Modified: 2012-06-27
I have the following situation here...
Main application is calling DLL (non COM) function:

long Func( wchar_t *, wchar_t **)

In this function, using first string I need to calculate something, allocate some memory for the second one, initialize it, so the client (from main app) can access this data. Obviously, the client has to free the allocated memory after performing certain task with it.

I have tried to do this using new/delete operators, but the main application was asserting on delete!
After this, I have tried malloc/free, but then it was failing in the debug version, on the free.
Can someone explain to me what is going on here and what is the best method to perform safe allocation/dealocation in this case?
0
Comment
Question by:ekc
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:Triskelion
ID: 6453717
Is your program attempting to access the allocated memory area AFTER the delete?  If so, that's a potential source of problems.
0
 
LVL 86

Expert Comment

by:jkr
ID: 6453749
>>I have tried to do this using new/delete operators, but
>>the main application was asserting on delete

You cannot free memory in a DLL that was allocated in an exe or vice versa, as usually both get their own allocator. There are two possible solutions:

- use the CRT runtime as a DLL (if you're using VC, go to the project settings, choose the C/C++ tab and change the settings in the 'Use run-time library' field available under 'Code Generation')

- make sure that memory is only deallocated in the module that allocated it...
0
 
LVL 5

Author Comment

by:ekc
ID: 6453833
> You cannot free memory in a DLL that was allocated in an exe or vice versa, as usually both get their
own allocator. There are two possible solutions:

How is that? DLL runs in-process, in the same thread with the code, executing it. It should be just another function call... Can you point me to some sources of information in this topic (MSDN, some link), please?
Sometimes, the client just doesn't know the size of memory to allocate, and it must be done in the DLL. Later, I can't obligate a client to call some other DLL function just to dealocate previosly allocated memory to avoid memory leak?!
0
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 6453939
>>How is that? DLL runs in-process, in the same thread
>>with the code

Yes, but if you're not using the Runtime as a DLL, both (or at least one) will get their own copy of 'new()' or 'malloc()'.

Let me guess - the assertion is at

        /*
         * 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));

For more information, see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dngenlib/html/heap3.asp
0
 
LVL 5

Author Comment

by:ekc
ID: 6454290
Looks like I have much to learn about NT memory management.
Thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now