problem with freeing memory in dll
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 37

Thread: problem with freeing memory in dll

Hybrid View

  1. #1
    Join Date
    Nov 2009
    Location
    California
    Posts
    31

    problem with freeing memory in dll

    i have a dll that exports a class - it uses an abstract base class with pure virtual functions(defined in a header file), then a function to export it that returns a new object. insided the class is a 'release' function that deletes the object. now in another dll of mine, i dont do anything with the deconstructor, but in this one for some reason i have to have the same 'delete this' as i do in the release function or it crashes. now in the host application is where i get some sort of problem. note - it runs/finishes without error but some code isnt being exectued or something.

    typedef IMyObj* (*PFNCreate)();
    //main
    char dllName[] = "dllname.dll";
    HMODULE hmod = LoadLibrary(dllName);
    if (!hmod)
    {
    cout << "error loading dll" << endl;
    return 0;
    }
    PFNCreate pfnCreate = (PFNCreate)GetProcAddress(hmod, \
    TEXT("CreateObject"));
    IMyObject* obj = (pfnCreate)();
    obj->function1();
    //some code
    cout << "??" << endl; //i get this outputted
    obj->release();
    delete obj;
    FreeLibrary(hmod);
    cout << "??"; //######## THIS DOESNT GET PRINTED
    return 0;

    thank you in advance, if more code is necessary to see what is going on let me know and ill post it.

  2. #2
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Wink Re: problem with freeing memory in dll

    Quote Originally Posted by rhboarder View Post
    obj->release();
    delete obj;
    Why are you using delete here? You said yourself that release() deletes the object.
    My hobby projects:
    www.rclsoftware.org.uk

  3. #3
    Join Date
    Nov 2007
    Location
    Birmingham, England
    Posts
    157

    Re: problem with freeing memory in dll

    I'm a bit confused by your code.

    You've gone through the effort of using dynamic access to the DLL. That is you used "LoadLibrary" and then "GetProcAddress" to find the function pointer to the "CreateObject" function.

    But your code doesnt do the same for the "release function".
    Is "release" a function pointer in the object?

    If so, can you check to see that its getting fired correctly.

    If not then could you explain how your code finds "release" but requires "GetProcAddress" to find "CreateObject"

    Regards
    Last edited by couling; November 12th, 2009 at 05:07 PM.
    Signature
    Please use: [ code ][/ code ] tags and reasonably correct tabbing. They really help us read your code
    End Signature

  4. #4
    Join Date
    Nov 2009
    Location
    California
    Posts
    31

    Re: problem with freeing memory in dll

    for the 1st reply: honestly i dont know why i needed it, i saw it done that way in a tutorial i read when learning how to export a class. and also the program crashes if i dont... 2nd reply - the release function is a class member in the class that's exported by CreateObject so you dont need to call getprocaddress for that. and i did check and it is getting fired correctly. here is a more complete code:
    //#### header file for the dll
    #if defined(DLL_EXPORT)
    #define MYAPI __declspec(dllexport)
    #else
    #define MYAPI __declspec(dllimport)
    #endif
    class IMyObj //abstract interface
    {
    public:
    //note - in sample tutorials ive read it says to have a deconstructor for the
    // interface class but whenever i add 'virtual ~IMyObj()=0;' it doesnt compile.
    virtual bool function1()=0;
    virtual bool function2()=0;
    virtual void release()=0;
    };

    extern "C" MYAPI IMyObj* __stdcall CreateObject();

    //#####in dll(.cpp) file
    #define DLL_EXPORT
    #include "myheaderfile.h"
    class myObj : public IMyObj
    {
    public:
    ~myObj()
    {
    cout << "deconstructor" << endl;
    delete this; //as i said earlier in another app i wrote this line
    //wasn't needed but now if i dont the program crashes
    }
    virtual bool function1()
    {
    //code
    }
    virtual void release()
    {
    delete this;
    }
    };
    IMyObj* __stdcall CreateObject()
    {
    return new myObj;
    }

    //#### and then i include this header in the file that contains the code i previously posted.

  5. #5
    Join Date
    Nov 2007
    Location
    Birmingham, England
    Posts
    157

    Re: problem with freeing memory in dll

    Quote Originally Posted by rhboarder View Post
    and also the program crashes if i dont...
    Any chance you could find what the error message is. If you're on windows vista you may have to go digging.

    It just all seems that something is not happening as it appears. Deleting an object twice will cause an error and this code is deleting obj twice by what you're telling me:
    Code:
    obj->release();
    delete obj;
    If you like I could take a look at your full source code (dll as well) and have a dig into what's happening. Which compiler are you using?
    Signature
    Please use: [ code ][/ code ] tags and reasonably correct tabbing. They really help us read your code
    End Signature

  6. #6
    Join Date
    Nov 2009
    Location
    California
    Posts
    31

    Re: problem with freeing memory in dll

    sorry bout the tags guys. i totally agree that deleting it twice should cause an error. but when i remove one i get another crash. For the compilor I'm using Borland 5.5 command line tools (bcc32). the error -
    (in ollydbg) - access violation when writing to -
    Code:
    3250308F  8943 08  MOV DWORD PTR DS:[EBX+8],EAX
    //and it says DS:[00000008] = ???
    do you have an email address you'd like me send it to couling?

  7. #7
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: problem with freeing memory in dll

    Quote Originally Posted by rhboarder View Post
    for the 1st reply: honestly i dont know why i needed it, i saw it done that way in a tutorial i read when learning how to export a class. and also the program crashes if i dont...
    The program will crash anyway!

    Quote Originally Posted by rhboarder View Post
    [code]
    ~myObj()
    {
    cout << "deconstructor" << endl;
    delete this;
    }

    virtual void release()
    {
    delete this;
    }
    Think about what happens when you call release(). The delete will correctly call the destructor, but then the delete in the destructor will also call the destructor, an infinite loop!

    So you need to remove the delete from the destructor and then find out the other reasons why the program crashes.
    My hobby projects:
    www.rclsoftware.org.uk

  8. #8
    Join Date
    Nov 2009
    Location
    California
    Posts
    31

    Re: problem with freeing memory in dll

    so i removed the delete from the deconstructor and the 'delete obj' in the hosting application so now it is:
    Code:
    IMyObj = (pfnCreate)();
    obj->function1();
    cout << "????" << endl;
    obj->realease();
    FreeLibrary(hmod);
    //this last cout gets printed but the program crashes right after 
    //it outputs it
    cout << "??????";
    return 0;
    function1 is good, it executes perfectly, and i checked and release is getting fired... still unable to figure out what could be causing it to crash.

  9. #9
    Join Date
    Nov 2009
    Location
    California
    Posts
    31

    Re: problem with freeing memory in dll

    ok so i think i've gotten it fully working without crashes now, although honestly i don't know why this works. ive defined the class member functions in the header as virtual, and then static loaded the lib file instead of using LoadLibrary with the dll. any thoughts on why using the static lib file is making the difference? i understand why/when to use a static lib but im just confused on why that would make it stop crashing compared to using LoadLibrary/FreeLibrary...
    Thanks for all the replies and help, much appreciated. i was so surprised to get responses so fast you guys rock!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center