Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Strange asertation

Posted on 2001-09-04
5
Medium Priority
?
331 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

670 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