Click here to Skip to main content
Click here to Skip to main content
Go to top

Using classes exported from dlls

, 13 Mar 2007
Rate this:
Please Sign up or sign in to vote.
How to dynamically use a class exported from a dll without error

Introduction

If you try to dynamically use a class exported from a .dll sometimes you get an ugly error because you try to release memory allocated by some other heap manager. For example, you create the object using a factory method and release it calling delete directly. In this case the heap is allocated by the .dll's heap manager and the .exe's heap manager throws an exception when you try to release it.

There are two methods to counter this problem. The easiest is to set Project -> Properties -> Code Generation -> Runtime Library to Multi-threaded Debug DLL both in the .exe and in the .dll.

If you can't do this for whatever reason you have a second option: you must free the memory in the same module (.dll or .exe) in which you have allocated it. The following code shows how to do it:

// d1.dll -> a1.h
#pragma once

#ifdef D1_EXPORTS
#define D1API __declspec(dllexport)
#else
#define D1API __declspec(dllimport)
#endif

class D1API A1
{
public:
    int x;
    static A1* New();
    static void Delete(A1* a1);
};

// d1.dll -> a1.cpp
#include "a1.h"

A1* A1::New()
{
    return new A1;
}

void A1::Delete(A1* a1)
{
    delete a1;
}

// e1.exe -> e1.cpp
#include "../d1/A1.h"

int main()
{
    A1 a1;
    a1.x = 10;

    A1* a2 = new A1; // allocates the memory using e1.exe-s heap manager
    a2->x = 20;
    delete a2; // correct, deletes the memory using e1.exe-s heap manager
    
    A1* a3 = A1::New(); // allocates the memory using d1.dll-s heap manager
    a3->x = 30;
    A1::Delete(a3); // correct, deletes the memory using d1.dll-s heap
                    // manager

    A1* a4 = new A1;
    a4->x = 40;
    A1::Delete(a4); // correct only if Runtime Library is set to 
                    //Multi-threaded Debug DLL

    A1* a5 = A1::New();
    a5->x = 50;
    delete a5; // correct only if Runtime Library is set to Multi-threaded 
               // Debug DLL

    return 0;
}

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication

Share

About the Author

Csupor Jenő
Software Developer
Romania Romania
No Biography provided

Comments and Discussions

 
GeneralAnother alternative - overriding operator new and operator delete PinmemberCsupor Jeno26-Apr-07 10:19 
GeneralThere is a similar article in the Visual Studio Help PinmemberCsupor Jeno21-Mar-07 7:37 
GeneralAlways match new with delete Pinmemberjeffb4213-Mar-07 10:50 
GeneralRe: Always match new with delete PinmemberCsupor Jeno14-Mar-07 2:30 
GeneralRe: Always match new with delete Pinmemberjeffb4214-Mar-07 12:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 13 Mar 2007
Article Copyright 2007 by Csupor Jenő
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid