C++ static destructor

This is a discussion on C++ static destructor within the C++ Programming forums, part of the General Programming Boards category; Before I get flamed too badly, yes I know there isn't a C++ static destructor. What I'm after is a ...

  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,796
    >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
    Portugal
    Posts
    7,578
    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
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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,344
    >> 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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    Portugal
    Posts
    7,578
    Are you sure isn't simply the output not reaching you? What sort of message?
    Try logging the destructor action to a file.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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,344
    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
    Portugal
    Posts
    7,578
    Wait. You are right of course. I was assuming he was noticing that behavior by calling clear on the vector.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    Portugal
    Posts
    7,578
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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,344
    >> 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, 04:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 02:22 PM
  3. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 06: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, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21