freeing memory used by a vector of pointer objects

Started by
7 comments, last by LostSource 18 years, 5 months ago
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 = ]
Advertisement
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.
Question: I'm sure its been asked, but is Boost compatible on Unix?
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.
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)
enum Bool { True, False, FileNotFound };
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?
Boost smart pointer library

It's got mostly everything you'll need.
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.
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
Thank you every one for the help on this. I'm defently going to be looking into boost.

This topic is closed to new replies.

Advertisement