Sign in to follow this  
LostSource

freeing memory used by a vector of pointer objects

Recommended Posts

Language: Cpp Compiler: MS Visual Studio.NET I just jump right into my question. Say we have this template struct:
template <typename T>
struct SFoo {
   T s_tfoo_data;  // Holds the foo data
   
   // holds the origin of where the foo data comes from
   std::vector< char* > s_hFooOrigin;

   // inline fuction that clears the foo origin vector
   inline void ClearFooOrigin() { s_hFooOrigin.clear(); }
};



Now say that we have template class:
template <typename T>
class CFoo {
public:
    // Default Constructor that sets the vector to NULL;
    CFoo() { m_hFooVector = NULL; }
    // Deconstructor
    ~CFoo()
private:
    // This is a vector of the SFoo<T> pointers that holds all the foo objects
    vector< SFoo<T>* > m_hFooVector;
};


My question is how would the deconstructor for the CFoo class look. What I thought it should look like, this that I should traverse through the vector m_hFooVector calling the ClearFooOrigin() function, then setting that to NULL and deleteing it. // PseudoCode
~CFoo() {
    std::vector< SFoo<T>* >::iterator itr

    for(itr = m_hFooVector.begin(); itr != m_hFooVector.end(); itr++) {
          (*itr)->ClearFooOrigin();

          (*itr) = NULL;

          delete (*itr);
    }
}
Is the deconstructor code going to free the memory used by the vector m_hFooVector? Or am I doing it wrong? I'm trying to stop a memory leak from happening by not free but the memory used by the vector. Thank you ahead of time for anyone that helps = ]

Share this post


Link to post
Share on other sites
delete pointers before you null them. Otherwise you aren't really deleting the pointer. And consider using a smart pointer like boost::shared_ptr instead of raw pointers. It makes things easier.

Share this post


Link to post
Share on other sites
The boost libraries can be used on most unix platforms. Almost certainly any one of the unix variants that recent versions of gcc have been ported to will have compatibility with most boost libraries.

Share this post


Link to post
Share on other sites
If the vector is the owner of the pointers, you can either use boost::shared_ptr, or, for less of a code hit, just make your own auto-delete smart pointer class, and make the vector be of that, instead of the raw pointer. (The size of a smart pointer is the same as a raw pointer)

Share this post


Link to post
Share on other sites
Quote:
Original post by LostSource
Unfortantly I haven't used smart pointers before. Is there a place that has a tutorials on smart pointers or even a tutorial on making a smart pointer class?

If you've never implemented a smart pointer before, it might be wise to just use boost's version (specifically shared_ptr in your case). If you decide a custom pointer class is best for you, there are many issues to take into account, like casting, copying symantics, auto-destruction (which I assume is why you'd consider boost::shared_ptr), etc. It will ship with any conforming compiler soon anyway, might as well get used to using it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this