Does STL vector automatically free content ?
-
I recently added STL vector to some 3D classes I have in order to store a huge amount of datas. I've read somewhere that vector doesn't free object untill it is freed. But I thought that myvector.clear() would free the content. I just experienced it doesn't: My classes are prototyped like this:
class C3dObject { public: C3dObject(TCHAR* pszObjectName); ~C3dObject(); std::vector m_vctVertex; }
In the destructor, I just call:m_vctVertex.clear();
and this frees nothing (The C3dVertex class has the same prototyping). Has someone an explanation to this ? Thanks Yarp http://www.senosoft.com/ -
I recently added STL vector to some 3D classes I have in order to store a huge amount of datas. I've read somewhere that vector doesn't free object untill it is freed. But I thought that myvector.clear() would free the content. I just experienced it doesn't: My classes are prototyped like this:
class C3dObject { public: C3dObject(TCHAR* pszObjectName); ~C3dObject(); std::vector m_vctVertex; }
In the destructor, I just call:m_vctVertex.clear();
and this frees nothing (The C3dVertex class has the same prototyping). Has someone an explanation to this ? Thanks Yarp http://www.senosoft.com/oops, my vector class is declared as follows (think it's due to tag like syntax in the vector's declaration):
std::vector<C3dVertex*> m_vctVertex;
Yarp http://www.senosoft.com/ -
I recently added STL vector to some 3D classes I have in order to store a huge amount of datas. I've read somewhere that vector doesn't free object untill it is freed. But I thought that myvector.clear() would free the content. I just experienced it doesn't: My classes are prototyped like this:
class C3dObject { public: C3dObject(TCHAR* pszObjectName); ~C3dObject(); std::vector m_vctVertex; }
In the destructor, I just call:m_vctVertex.clear();
and this frees nothing (The C3dVertex class has the same prototyping). Has someone an explanation to this ? Thanks Yarp http://www.senosoft.com/vector doesn't free anything because the things it's storing aren't objects. You're storing pointers. You'd need to modify it to be: for( it = m_vctVertex.begin(); it != m_vctVertex.end(); ++it) { if (*it) delete (*it); } m_vctVertex.clear(); where it is an iterator, as usual. If the thing was an object itself, eg CString or struct xxx or whatever, then it would do what you expect. Steve S
-
I recently added STL vector to some 3D classes I have in order to store a huge amount of datas. I've read somewhere that vector doesn't free object untill it is freed. But I thought that myvector.clear() would free the content. I just experienced it doesn't: My classes are prototyped like this:
class C3dObject { public: C3dObject(TCHAR* pszObjectName); ~C3dObject(); std::vector m_vctVertex; }
In the destructor, I just call:m_vctVertex.clear();
and this frees nothing (The C3dVertex class has the same prototyping). Has someone an explanation to this ? Thanks Yarp http://www.senosoft.com/Take a look at this article[^].
-
Take a look at this article[^].
Thank you both for your explanation. I thought this had something to do with pointer based vector, I understand why now... and btw Nemanja your article is very interesting. STL obviously is a very powerfull concept. Yarp http://www.senosoft.com/