Thread: memory Leak - Cannot sort it

  1. #1
    Venet
    Guest

    memory Leak - Cannot sort it

    Hi All,

    I have a function contained in a DLL and one of its parameters is a pointer to a struct that will be filled in with some information.

    // struct definition
    struct SomeStruct
    {
    char *one;
    char *two;
    }

    //function in DLL
    // DLL Headers go here
    int GetInfo(int a, int b, SomeStruct *myS)
    {
    // some code goes here

    for(int i = 0; <some condition>; i++)
    {
    myS[i].one = new char[100];
    myS[i].two = new char [100];

    //fill myS[i].one and myS[i].two with some data
    }
    } //end of DLL function


    //Main Function
    //Some variables are declared here and main()

    SomeStruct *myInfo;
    myInfo = new SomeStruct[100]; // Create 100 instances

    // Function returns how many myInfo were filled in
    int nRet = GetInfo(1, 2, myInfo);

    for(int j = 0; j < nRet; j++)
    {
    cout << myInfo[j].one << " and " << myInfo[j].two;
    }

    delete myInfo;

    //exit main function and the app as well...



    The Problem:

    I don't know how to delete the instances of char created in the DLL.
    myS[i].one = new char[100];
    myS[i].two = new char [100];

    I cannot delete them in the DLL function as I need them in the EXE, however when I try to delete them in EXE, cannot do that as these pointers were created by the DLL module.

    So how can I prevent this memory leak? Is there any way to delete these pointers? If so how? Or is this a bad design overall? any suggestions?

    I appreciate your help!

    Venet.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793

    Re: memory Leak - Cannot sort it

    Try allocate the space with HeapAlloc()....

    As long as you free the memory from the same heap you draw it from (use GetProcessHeap() or create your own) you should be able to allocate it in the dll and then free it in the main exe...

    Though why cant you allocate the memory in the main exe? It would make life more straight forward.....personally I prefer to keep allocation and deletion in the same place.....

  3. #3
    Venet
    Guest
    Thanks for your reply.

    But doesn't DLL and EXE both have different Heaps? How about the case when the DLL is loaded at run time ?

    Creating my own heap, would things a bit messy.

    I suppose I could add a consturctor to the struct and initialise its member variables and the struct itself?

    What's your opinion on this approach ?

    Thanks
    Venet.

  4. #4
    Venet
    Guest
    Thanks for your reply.

    But doesn't DLL and EXE both have different Heaps? How about the case when the DLL is loaded at run time ?

    Creating my own heap, would make things a bit messy.

    I suppose I could add a consturctor to the struct and initialise its member variables and the struct itself?

    What's your opinion on this approach ?

    Thanks
    Venet.

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by Venet
    Thanks for your reply.

    But doesn't DLL and EXE both have different Heaps? How about the case when the DLL is loaded at run time ?

    Creating my own heap, would things a bit messy.

    I suppose I could add a consturctor to the struct and initialise its member variables and the struct itself?

    What's your opinion on this approach ?

    Thanks
    Venet.
    As your exe AND all dlls are in th same process space, then they can share the process's default heap......

    You can make a new heap quite easily (if you wish) with HeapCreate() and then kill it with HeapDestroy()....but often its easier to use the default heap

    It might be an idea as you said, to create a class that sorts the memory allocation & freeing and then export that from the dll......you can than make an instance in your exe....tell it to allocate the memory....and then that class could then be responsible for the cleaning up via its destructor when it goes out of scope

  6. #6
    Unregistered
    Guest
    Considering that I'm quite new to the Win32 programming, what's the best way to pass a struct that will be filled in by a function, like some of Win32 Functions do ?

    e.g. VOID GetSystemInfo(LPSYSTEM_INFO lpSystemInfo)

    receieves a pointer to a structure and fills it in.

    Thanks again
    Venet.

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by Unregistered
    Considering that I'm quite new to the Win32 programming, what's the best way to pass a struct that will be filled in by a function, like some of Win32 Functions do ?

    e.g. VOID GetSystemInfo(LPSYSTEM_INFO lpSystemInfo)

    receieves a pointer to a structure and fills it in.

    Thanks again
    Venet.
    Yeah....a pointer is the best way to do it...then you cave complete control of the struct from where you pass it to the func

    Also, personally I prefer to have a return.....I usually use BOOL to record success or failure......I find it makes my debugging a lot easier....a lot of people are fine with void........in the end its a matter of preference

    Good luck!

  8. #8
    Venet
    Guest
    Thanks for your help

    Regards,
    Venet

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leak with detached pthreads - how to free?
    By rfk in forum Linux Programming
    Replies: 2
    Last Post: 08-17-2007, 06:50 AM
  2. Is this code memory leak free? ---> POSIX Threads
    By avalanche333 in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2007, 03:19 PM
  3. Memory Leak Help
    By (TNT) in forum Windows Programming
    Replies: 3
    Last Post: 06-19-2006, 11:22 AM
  4. Huge memory leak
    By durban in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2005, 12:10 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM