Thread: C++ static destructor

  1. #1
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234

    C++ static destructor

    Before I get flamed too badly, yes I know there isn't a C++ static destructor.

    What I'm after is a technique to clean-up after a class that consists entirely of static member functions and static data. The class maintains a vector of class pointers in its private area. I want the ability to guarantee that the vector is emptied and the class pointers are deleted (hence calling the class' destructors).

    The class is actually in a Windows DLL, so I guess there is a function that is called when a DLL is unloaded? If so, I could call a static pseudo-destructor of my own design to clean-up?

    I know the memory would be free'd anyway when the DLL unloads, but I wanted a more elegant solution.

    I was toying with the idea of a singleton model, but the class is really a wrapper for a whole set of utility functions and I think a singleton model is probably overboard.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I want the ability to guarantee that the vector is emptied and the class
    >pointers are deleted (hence calling the class' destructors).
    Wrap the vector in a class and have that destructor handle the release of your pointers. Or you could use smart pointers.
    My best code is written with the delete key.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I'm not sure how much of a problem this is. I'm far from being an expert, but I believe your static data members will be destroyed after main(). At that time, the vector contents class destructors will be called regardless, ensuring proper deallocation. Assuming the deallocation is being properly coded inside the destructor all will be ok... and even better if you implement smart pointers.

    My shaky 2 cents
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> the vector contents class destructors will be called regardless, ensuring proper deallocation.
    If the vector is holding raw pointers, the "destructor" of a raw pointer does nothing and does not properly destroy what it points to. Use of ptr_vector from Boost, or using a vector of shared_ptrs, or wrapping the vector in a class that does the deletion for you (which is exactly what ptr_vector does) are really the best bets.

    I do believe there might be a function called when unloading the dll that might also be helpful, but I'm not sure.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I do believe there might be a function called when unloading the dll that might also be helpful, but I'm not sure.
    You mean dllMain?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    The vector holds pointers to a specific class type. I'd assumed that delete'ing the pointer would call the appropriate destructor. I put a message in the destructor and if you specifically delete each element in the vector, the destructor appears to be called OK. But if you don't specifically call the destructors (leaving the DLL to clean-up), the class destructor isn't called.

    I don't want to have to instantiate the class, it only contains static data and static functions. It's just a convenience to wrap the functions in a class in the first place, to logically group them.

    I'm not sure what wrapping the (static) class in another class is going to achieve?

    I'll have a look at smart pointers.
    Last edited by SKeane; 11-17-2006 at 10:31 AM.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Are you sure isn't simply the output not reaching you? What sort of message?
    Try logging the destructor action to a file.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Instead of using all static functions, you can make that class a singleton. If you can ensure that the singleton's destructor is called, then in that destructor you can clean up your pointers. You're not actually wrapping your "static" class in another class, you are just changing your static class to clean itself up.

    >> Are you sure isn't simply the output not reaching you? What sort of message?
    The destructors won't be called if you don't delete the pointers. SKeane's observed behavior is consistent with what is supposed to happen.

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Wait. You are right of course. I was assuming he was noticing that behavior by calling clear on the vector.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Hmm... Daved?
    ... but wouldn't exiting the application call the destructors after main()?

    Regardless, making the class containing the statics a singleton seems the natural thing to do.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> but wouldn't exiting the application call the destructors after main()?
    Which destructors? It will call the destructors for static objects. If the vector is a static member of the class, the vector destructor will be called after main. It will not call the destructor for objects allocated with new, so the pointers inside the vector will not have the destructors of the objects they point to called automatically.

  12. #12
    Registered User
    Join Date
    Nov 2006
    Posts
    6
    Quote Originally Posted by Daved
    Instead of using all static functions, you can make that class a singleton. If you can ensure that the singleton's destructor is called, then in that destructor you can clean up your pointers. You're not actually wrapping your "static" class in another class, you are just changing your static class to clean itself up.
    Change all the functions to non-static and declare an object of that class. They will be accessed as normal, except with ObjectName.member instead of ClassName.member, but you can easily just change the names so that it works the same. Then of course use the destructor to do your clean up.

  13. #13
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Thanks for the advice. I really should go down the singleton route, but here's what I finally did.

    The DLL exports a whole class (the interface). This class' destructor calls a static "pseudo" destructor for the class with all static functions. The class with all static functions uses lazy initialization to load its static data table (a vector of pointers to a class). The class with all static functions has a clean-up routine, which is its "pseudo" destructor. Thus everything is cleaned up. If it weren't for the "outer" class being in a position to call the clean-up routine, I would have invested a bit more energy in making the all static functions class a singleton.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  4. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM