Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6163001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:45:10+00:00 2026-05-23T21:45:10+00:00

I need some clarification regarding runtime/heap issues when deleting an object which was created

  • 0

I need some clarification regarding runtime/heap issues when deleting an object which was created in a DLL. It needs some introduction before I come to my questions…

In my project a DLL (which is specified by its name) returns a new Grabber object. In an earlier version of my code, the DLL exported a function like this:

extern "C"
__declspec(dllexport) Grabber* CreateGrabber(string settings)
{
    return new SomeSpecificGrabber(settings);
}

In the EXE I used a static function like this to create a new Grabber object:

static Grabber* createGrabberObject(const std::string& grabberType, const std::string& grabberSettings)
{
    FARPROC hProc = 0;

    // load dll with the name of grabberType
    HMODULE hDLL = LoadLibrary(grabberType.c_str());

    // get address for CreateGrabber function
    hProc = GetProcAddress(hDLL, "CreateGrabber");

    // instantiate a function pointer of our type and typecast the address
    // of the CreateGrabber function to this type
    CreateGrabberFunctionType CreateGrabberFunction = (CreateGrabberFunctionType)hProc;

    // call CreateGrabber in DLL to get a Grabber object
    return CreateGrabberFunction(grabberSettings);
}

In the EXE the lifetime of a Grabber object is managed by a smart pointer:

shared_ptr<Grabber> myGrabberObj = shared_ptr<Grabber>(createGrabberObject("SomeGrabber.DLL", "Settings"));

This all worked fine as long as I compiled the EXE and the DLL with the /MDd setting (VC++ 2010), which means that EXE and DLL used the same heap.

Now I want to compile my solution with the /MTd setting. With this I got a runtime assert of type _CrtIsValidHeapPointer for the settings string object I passed to the DLL. This makes sense because the DLL tries to delete a string object which was created in the EXE. And they don’t use the same heap anymore.

I got around this problem by changing the exported DLL function a little bit (const char* instead of string):

extern "C"
__declspec(dllexport) Grabber* CreateGrabber(const char* settings)
{
    return new SomeSpecificGrabber(settings);
}

And in createGrabberObject I pass grabberSettings.c_str() instead of grabberSettings to the DLL function.

Now everything works fine again. But now comes my first question: Why don’t I get the _CrtIsValidHeapPointer assertion when myGrabberObj is deleted? The object was created from within the DLL but is deleted from within the EXE (by the smart pointer). Why don’t I have the same problem here as with the string object above?

I guess a clean solution would be that the DLL also exports a function like this:

extern "C"
__declspec(dllexport) void DeleteGrabber(Grabber* grabber)
{
    delete grabber;
}

Then I would also have a static function in my EXE which calls DeleteGrabber in a DLL:

static void deleteGrabberObject(const std::string& grabberType, Grabber* grabber)
{
    FARPROC hProc = 0;

    // load dll with the name of grabberType
    HMODULE hDLL = LoadLibrary(grabberType.c_str());

    // get address for DeleteGrabber function
    hProc = GetProcAddress(hDLL, "DeleteGrabber");

    // instantiate a function pointer of our type and typecast the address
    // of the DeleteGrabber function to this type
    DeleteGrabberFunctionType DeleteGrabberFunction = (DeleteGrabberFunctionType)hProc;

    // call DeleteGrabber in DLL
    DeleteGrabberFunction(grabber);
}

This static function could then automatically be called by the smart pointer:

shared_ptr<Grabber> myGrabberObj = shared_ptr<Grabber>(createGrabberObject("SomeGrabber.DLL", "Settings"), 
boost::bind(deleteGrabberObject, "SomeGrabber.DLL", _1));

This also works. But here comes my second question: The static functions createGrabberObject and deleteGrabberObject both load the DLL. Does it mean that two different heaps are created because two instances of the DLL are loaded (then this solution would not solve my problem at all)? Or do these two static functions use the same heap?

I hope someone can explain what’s going on here…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:45:10+00:00Added an answer on May 23, 2026 at 9:45 pm

    The DLL is reference counted, not loaded twice, and when you use LoadLibrary then it’s only loaded once anyway and they will use the same heap. The static function is the normal solution to this problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some clarification. I have a Reportwriter dll that uses Crystal Reports. It
I need some clarification/advice regarding modifying objects stored in mutable containers in Objective-C. Let's
I have a basic question for which I need a some clarification in it's
I just need some clarification on variables A normal variable has 2 parts to
Need some help about with Memcache. I have created a class and want to
I'm in need of some clarification. I've been reading about REST, and building RESTful
Just need some clarification on how to design a python script file test.py. When
I think I need some clarifications regarding WPFs Dispatcher.Invoke and Dispatcher.BeginInvoke usage. Suppose I
I need some clarification about the behaviour of svn switch. I'm using SVN version
I need some clarification. Are these two methods the same or different? I get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.