Thread: If my DLL return an array, where do I free it?

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    163

    If my DLL return an array, where do I free it?

    I create a DLL, which return a structure
    Code:
    struct opStructure {
    	int num;
    	int **motif1;
    	int **motif2;
    };
    The DLL allocates memory for motif1 and motif2, return opStructure to the program using this DLL. But how to I free this new memory of motif1 and motif2? I try to free this 2 memory in the program usinng this DLL but have error. Or I do not need to free them? DLL itself will free it??

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    When you return your pointer, you can free it using delete keyword. For every new keyword you use, there should be a delete pair. If you don't use new there is no need to free it.
    Show your code for allocating an freeing memory.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Never free memory allocated in one module in a different module.

    Three choices.
    1) Pass in enough memory for the DLL to use, so that it doesn't have to allocate.
    2) Create a DLL function that you pass the struct and it frees the memory.
    3) Since this is C++, why not use a std::vector or a boost::multi_array instead of managing the memory yourself?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    Quote Originally Posted by CornedBee
    Never free memory allocated in one module in a different module.

    Three choices.
    1) Pass in enough memory for the DLL to use, so that it doesn't have to allocate.
    2) Create a DLL function that you pass the struct and it frees the memory.
    3) Since this is C++, why not use a std::vector or a boost::multi_array instead of managing the memory yourself?
    This is the headache I'm facing. I'm allocating memory in DLL and freeing it in the program that is using the DLL. So far, it works alright. I agree it is risky, but for your first suggestion, I can't do it because the memory is dynamic, I will never know the exact memory size needed.

    For your second suggestion, it's not possible because the if I free the memory in DLL, then my main program can't receive the array from DLL


    For you third suggestion, I think it will have the first problem as the second suggestion

    Below is my code which I call the DLL. minemqb is the DLL function. I'm passing in a pointer chain_interact_result, which the function minemqb will point it to the results I needed

    Code:
    minemqb(&chain_interact_result, db, protein[protein_index].chain[chain1][0]+1, protein[protein_index].chain[chain2][0]+1, 0, 2, 0, 2, 0);
    Last edited by franziss; 10-03-2006 at 03:30 AM.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, you mustunderstood all three of my suggestions.

    1) If the client allocates the memory, obviously the DLL needs a way of first reporting how much memory it will need.

    2) If the DLL allocates the memory, it doesn't free it immediately. It frees it when the client calls a function that is explicitely documented to do that.
    In other words, the function that returns the opStructure contains in its docs something like:
    "The returned opStructure must be passed to FreeOpStructure when the client is done with it."

    3) You're wrong. I don't know what else to say.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    Quote Originally Posted by CornedBee
    2) If the DLL allocates the memory, it doesn't free it immediately. It frees it when the client calls a function that is explicitely documented to do that.
    In other words, the function that returns the opStructure contains in its docs something like:
    "The returned opStructure must be passed to FreeOpStructure when the client is done with it."
    Oic, I go and try it out now. When does the DLL free memory? When the client program cease? Or when the DLL function return control to the client program?

    YES! it works! thanks cornedbee! Lesson learnt: The DLL only cease when the client program cease =)
    Last edited by franziss; 10-03-2006 at 03:47 AM.

  7. #7
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    The DLL frees (client specific) memory when it is unloaded either by (a) an explicit command to unload it, or (b) when the client exits.

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    what is the explicit command to unload DLL when my client program is still running?

  9. #9
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    In Windows? If you had used LoadLibrary()/LoadLibraryEx() to load your DLL, you'd use FreeLibrary() to release it (well reduce the ref count anyway). If you didn't explicitly load the library, you can't (safely) explicitly unload it. AFAIK

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  4. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM
  5. string class errors
    By CodeMonkey in forum C++ Programming
    Replies: 13
    Last Post: 07-20-2003, 11:20 PM