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

x
?
Solved

unable to delete Dll memory from an executable

Posted on 2008-06-18
2
Medium Priority
?
240 Views
Last Modified: 2013-12-14
Hi,
Following is the situation:
I have a dynamic library which has a static method and creates and returns a pointer to the class  object.
I load this library from an executable, call the static method and try deleting the pointer. It complains with the following:
Debug assertion failed.
Expression: _CrtValidHeapPointer(pUserData)

I need to know why is this happening?
Please guide to me any relevant article which might be useful for understanding this topic.

cheers,
tushar.


// Header file for my library : mylib.h
 
#ifndef MYLIB_H
#define MYLIB_H
 
#ifdef MYLIB_EXPORTS
	#define DllExport __declspec(dllexport)
#else
	#define DllExport __declspec(dllimport)
#endif // MYLIB_EXPORTS
 
 
class MyLib
	{
	public:
		DllExport static MyLib* New();
		DllExport ~MyLib();
	private:
		DllExport MyLib();
	};
 
 
//Implementation of mylib.cpp//
DllExport MyLib* MyLib::New()
	{
	MyLib* mylib = new MyLib();
	return mylib;
	}
 
MyLib::MyLib()
	{}
 
MyLib::~MyLib()
	{}
 
// Executable implementation
#include "mylib.h"
 
int main()
{
MyLib* mylib = MyLib::New();
delete mylib; // this delete gives problem
}

Open in new window

0
Comment
Question by:tusharganguli
[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 Comments
 
LVL 40

Accepted Solution

by:
evilrix earned 750 total points
ID: 21820235
You can only delete heap in the module it was allocated. This is because each module has its own heap. Uf you've allocated the heap in the DLL the DLL should also expose a function to allow pointer to the heap to be pass back in so it can be released.

http://blogs.msdn.com/oldnewthing/archive/2006/09/15/755966.aspx
0
 
LVL 2

Author Closing Comment

by:tusharganguli
ID: 31468671
Thanks a lot for the explanation, and apologies for not aceepting this solution earlier, just have lost touch with EE for a long while now :-). Although I did understand the run time concept of memory allocation, but if you could still provide some links for understanding why is there such a discrepancy? what exactly do we mean by runtime being different? is this a phenomena only with windows dll or even linux libraries have these problems?
Would appreciate any further links or recommendation of books, I am eager to know what exactly is happening here.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.
Suggested Courses

660 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