Archived

This topic is now archived and is closed to further replies.

browny

a STL question

Recommended Posts

suppose i have a class
class C3DObject{
// blah blah


CVertex *m_pVertices

C3DObject()
{
    m_pVertices = NULL;
}
~C3DObject()
{
    if(m_pVertices) delete [] m_pVertices;
}

}
Now if i save and add these C3DObject[s] in a STL vector is there any chance of the program crashing due to the desctrutor that i have written. I''m working on a game engine and i have been working on a WEIRD bug for the last two days until i narrowed down to this particular place. Interestingly if i remove that "delete [] m_pVertices" the program runs fine. I dubugged the program until it crashes and then opened up the "Call Stack window" where it shows the the destructor has been called by the "vector.push_back()" functions. This is probably because the vector class is reallocting memories. But the question is.. while reallocting memories.. does STL vector "delete" the objects in the vector already before or after copying them to a new location ? Now if that is the case.. then let A is an instance of the class C3DObject and the arrow below represents the m_pVertices pointer A-> some memroy location after executing STLvector.push_back() destructor of A is called [ i am assuming ] A-> NULL [ memory has been released according to the code above ] now if STLvector copies [ shallow copy ] the content of A to a new location , this new A will have an invalid pointer because the memory location previously pointed to by A has been released !!!! Is that the reason why the whole engine is crashing ?

Share this post


Link to post
Share on other sites
Your class don't have the requirments for a STL container, it must have at least

"No argument" constructor:
MyObject::MyObject()

Copy constructor:
MyObject::MyObject(const MyObject& m)

Copy assignment operator:
const MyObject& MyObject::operator= (const MyObject& right)

quote:
Is that the reason why the whole engine is crashing ?
Yes

[edited by - noVum on August 14, 2003 9:56:11 AM]

Share this post


Link to post
Share on other sites
O DAMMIT... [ out of frustation ] ...
Thanks a lot noVum...

You just wouldn't believe; i have been working on the engine code for the last two days [ in a row ] to fix this problem... and ofcourse from almost some thousand lines of codes. Just like trying to find a needle form haystalk. Plus ,this section of the code wasn't written by me so it was even more difficult to actually guess where the error might percolate from.

Are there any other basic requirements for STL because i never use STL; i usually write my own list and other container classes.

[edited by - browny on August 14, 2003 10:09:42 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by noVum
Your class don''t have the requirments for a STL container, it must have at least

"No argument" constructor:
MyObject::MyObject()
I''m pretty sure this isn''t necessary. You only need it if you''re going to "resize" it to a larger size without the second argument.

I.e. This wouldn''t be OK for Foo that has no default constructor:

std::vector<Foo> arr;
arr.resize(100);

But this would:

std::vector<Foo> arr;
arr.resize(100, Foo("Yahoo!"));

As well as all other vector operations. Same for other containers.

But there may be some flawed STL implementations that do require default constructor as well.

Share this post


Link to post
Share on other sites