Solved

unable to delete Dll memory from an executable

Posted on 2008-06-18
2
234 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
2 Comments
 
LVL 40

Accepted Solution

by:
evilrix earned 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel/Word Add-in in what language? 5 107
typedef std::deque<BYTE> ByteVector is broken in vs2012 23 82
c++ syntax question 9 43
oracle 11g 23 46
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…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

932 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

12 Experts available now in Live!

Get 1:1 Help Now