Solved

Strange asertation

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

758 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

23 Experts available now in Live!

Get 1:1 Help Now