Thread: DLL Memory

  1. #1
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346

    DLL Memory

    I read something about this a long time ago and now I cannot remember the details. If I allocate memory using malloc or GlobalAlloc in a DLL, can I free it in my executable? And vice-versa?

    Thanks

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  2. #2
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Only if you can be sure if the application is using the same memory manager as that used in the DLL. Generally, the answer should be no. Although you will get away with it if the DLL and application are witten with the same compiler (and probably the same version of compiler).

    I.e. suppose I write a DLL using VC++ 6, which I use in a program written with Borland C++ Builder. If I have a pointer to a resource which is created by the DLL, I should not try to free it from the application directly (i.e. by calling delete or free). The results could be disasterous.

    Instead, what I need in the DLL is a function which frees the resource for the application. I.e.

    FreeCustomResource(CustomResource* cr)
    {
    delete cr;
    }

    If you ever look at COM or other object packaging technologies, you will always see object creator and destroyer functions provided by the DLL. In that way it can be ensured that objects (or resources) and created & destroyed by the same memory manager.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  2. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  3. Using class with DLL
    By greg2 in forum C++ Programming
    Replies: 2
    Last Post: 09-12-2003, 05:24 AM
  4. .lib vs .h vs .dll
    By Shadow12345 in forum C++ Programming
    Replies: 13
    Last Post: 01-01-2003, 05:29 AM
  5. Passing parameters from VB to C++ through ActiveX DLL
    By torbjorn in forum Windows Programming
    Replies: 0
    Last Post: 12-10-2002, 03:13 AM