Solved

Strange asertation

Posted on 2001-09-04
5
282 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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

809 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