Jump to content
  • Advertisement
Sign in to follow this  
Leadorn

dll alloc deallocation

This topic is 4834 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi To alloc memory in a dll and dealloc it in another dll or exe will throw an exception. Is is possible to get around this, can you implement a mechanism that makes it possible to alloc and dealloc in different dlls or exe's. What could be the risks, why haven’t anybody solved this yet. I’ve tried to have a local function pointer that will be initialized from the exe. The new operator is implemented in the class residing in the dll and will call this local function pointer. When new is called on this class it will invoke a function residing in the exe file, and this function will call malloc. Dll Get_class() -> new dllclass -> operator new -> resource_alloc = asd -> asd -> malloc()

#include <windows.h>
#include "../testdll/dllclass.h"


void *asd(const size_t size)
{
	return malloc(size);
}


int main()
{
	HMODULE dll;

	dll = LoadLibrary("testdll");

	//GetProcAddress(dll,"");


	adt::darray<int> list;
	dllclass c;

	dllclass::set_resource_alloc(asd);

	dllclass *cl = dllclass::get_class();
	delete cl;

	FreeLibrary(dll);
}





#pragma once

#include "adt_darray.h"




typedef void * (*RESOURCE_ALLOC)(const size_t);





class __declspec(dllexport) dllclass
{
public:

	static dllclass *get_class();

	dllclass(void);
	~dllclass(void);

	static void set_resource_alloc(RESOURCE_ALLOC m);
	

	void *operator new(const size_t size);


	char *get_data();

	void start(adt::darray<int> &list);
	void stop();
};

cpp file 


#include "stdafx.h"
#include "dllclass.h"

RESOURCE_ALLOC resource_alloc = 0;



dllclass::dllclass(void)
{
}

dllclass::~dllclass(void)
{
}


void dllclass::set_resource_alloc(RESOURCE_ALLOC m)
{
	resource_alloc = m;

}


dllclass *dllclass::get_class()
{
	return new dllclass;
}

void *dllclass::operator new(const size_t size)
{
	return resource_alloc(size);
}



char *dllclass::get_data()
{
	return new char[40];

}



void dllclass::start(adt::darray<int> &list)
{
	for(int i = 0;i < list.size();i++)
	{

	}
}

void dllclass::stop()
{

}








Share this post


Link to post
Share on other sites
Advertisement
Hi,

The problem is that the application and the DLL have different heaps. What I usually do is:


class SharedClass
{
private:

SharedClass(void);
virtual ~SharedClass(void);

public:

static SharedClass* Create(void);

virtual void Release(void);
};

// ..

SharedClass* SharedClass::Create(void)
{
return new SharedClass();
}

void SharedClass::Release(void)
{
delete this;
}



I made the constructor and destructor private so that you have to go through the Create() and Release() methods. Release() is virtual to make sure that when you call it, it will go through the virtual table and the call will be executed in the DLL that created the instance of the class.

Same thing, if you need to do allocation and deallocation within the class, do that in virtual functions so that everything that is memory management related will be done on the same heap.

There may be some better ways to do this, but it's working well for me..

Share this post


Link to post
Share on other sites
one small correction, I need to overload the global new operator, for example adt::string uses new too.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!