Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Strange asertation

Posted on 2001-09-04
5
Medium Priority
?
340 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 400 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

916 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