It is currently Fri Dec 15, 2017 8:40 pm

All times are UTC - 5 hours




 Page 1 of 1 [ 7 posts ] 
Author Message
 Post subject: Scene::DeleteAllXXX() required?
PostPosted: Thu Aug 19, 2010 10:01 am 

Joined: Wed Dec 24, 2008 4:25 pm
Posts: 16
I was chasing down a bug caused by a typo and made an interesting "discovery". When std::vector<Type*> is deleted, it first deletes all its contents. Given that, are all the DeleteAllXXX() functions of any real use? I mean DeleteAllTextures(), DeleteAllFonts(), DeleteAllGeometries(), etc. Assuming the answer is no, I commented out all their code, leaving only empty functions (saving the code in case my theory was all wet) and everything works fine. (I'm up to VMK24ABC, so there may be a surprise in store for me later.)

Am I all wet, or are these functions superfluous?


Offline
 Profile  
 
 Post subject:
PostPosted: Sun Aug 29, 2010 6:31 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
Hi Daryl,
Thanks for the comment. I wrote a simple test program to verify the behaviour of std::vector. My program looks like this:

TestClass.h
class TestClass {
public:
   TestClass();   
   ~TestClass();
protected:   
private:   

//---------- functions above -----|----- variables below -----------------
public:   
protected:   
private:

};//TestClass


TestClass.cpp
#include "TestClass.h"

//------------------------------------------------------------------------
// Name: TestClass(:)
// Desc: constructor
//
TestClass::TestClass() {
   printf( "construct\n" );
} //TestClass

//------------------------------------------------------------------------
// Name: ~TestClass(:)
// Desc: destructor
//
TestClass::~TestClass() {
   printf( "destroy\n" );
} //~TestClass


main.cpp
int main( int argc, char** argv ) {      

   std::vector<TestClass*> v;

   v.push_back( new TestClass );

   printf( "return 0\n" );   
   return 0;

}//main


When I run this code, I see the following output:
construct
return 0
Press any key to continue . . .


Notice that the destructor is never called. I also tried adding in v.clear(); before the return but that didn't call the destructor either.

Are you seeing something different?


Offline
 Profile  
 
 Post subject: You're right, of course!
PostPosted: Sun Aug 29, 2010 3:03 pm 

Joined: Wed Dec 24, 2008 4:25 pm
Posts: 16
That's what I get for believing what I read on the Internet! You're right, of course. I should've checked that for myself before posting the inquiry. Thanks!


Offline
 Profile  
 
 Post subject:
PostPosted: Sun Aug 29, 2010 8:52 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
What I found very interesting is what happens if you try to save TestClass (not a pointer) inside std::vector. Changing main to look like this:

int main(int argc, char** argv) {      

   std::vector<TestClass> v;

   TestClass t;

   v.push_back( t );

   printf( "return\n" );   
   return 0;

}//main


The output is:
construct
destroy
return
destroy
destroy


I wonder what is going on here, why is the destructor called 3 times instead of the 2 times that I was expecting.


Offline
 Profile  
 
 Post subject: 2005 v. 2008 & GCC
PostPosted: Mon Aug 30, 2010 3:59 pm 

Joined: Wed Dec 24, 2008 4:25 pm
Posts: 16
I ran your odd example under the GCC compiler, VS 2008, and VS 2005. The three 'destroy' calls occurs only in VS 2005; I suspect that's another bug that M$ has since fixed. But I still find it odd that even with the other two compilers, the destructore is still called twice:

construct
return
destroy
destroy

In any case, it's now off my list of worry sources. :)


Offline
 Profile  
 
 Post subject: Why there are so so many dtors? I'll tell you why! :)
PostPosted: Wed Sep 22, 2010 12:53 pm 

Joined: Wed Aug 06, 2008 7:53 pm
Posts: 182
Location: Russia
Hi, guys!
It seems I have found a true answer to this question. At least I hope so. That’s pretty unexpected and funny.
Marek wrote:
I wonder what is going on here, why is the destructor called 3 times instead of the 2 times that I was expecting.

Daryl wrote:
The three 'destroy' calls occurs only in VS 2005; I suspect that's another bug that M$ has since fixed. But I still find it odd that even with the other two compilers, the destructore is still called twice.


Basically, there are two questions:
    1) Why is it so that the number of constructor calls does not match the number of destructor calls?
    2) Why on Earth M$ VC 2005 (and only it!!) indeed generates destructors' calls 3 times?


The answers.
1) The missing constructors are actually so called copy-constructors that compiler generates for you if you do not present your own version. You may add copy-constructor to the TestClass and you will see the missing link constructors’ calls:
TestClass( const TestClass& ) { printf( "I’m a copy-ctor" ); }


A complete example with static counter:
//
// main.cpp
//
#include <vector>
#include <iostream>

using namespace std;

class A
{
    static int counter;

public:
    A() : n( counter++ ) { cout << "default ctor #" << n << endl; }
    ~A() { cout << "dtor #" << n << endl; }

    A( const A& ) : n( counter++ ) { cout << "copy ctor #" << n << endl; }
//  A& operator= ( const A& ){ cout << "operator= #" << n; return *this; }

private:
    int n;
};

int A::counter = 0;

void test()
{
    vector<A> v;
    A a;

    v.push_back( a );

    cout << "return" << endl;
}

int main(int argc, char* argv[])
{
    test();
    return 0;
}


2) Second question is more intricate. I have traced the source code up to the assembly level and occasionally caught the fish :-) (my dictionary says: ≈ smb needs it like a fish needs a bicycle :-) )
Daryl, you should not blame the M$. M$ has not created STL code, but provides and supports the one written by DINKUMWARE. The recent versions of C++Builder by CodeGear (aka Embarcadero => former Inprise => former famous Borland) also use Drinkumware’s STL (the code originated in HP).

It happened that STL version of MS VC2005 has
vector::push_back() that calls eventually ->
vector::insert(iterator _Where, const _Ty& _Val) ->
vector::_Insert_n(iterator _Where, size_type _Count, const _Ty& _Val).

The _Insert_n() in VC2005 has follwing unfortunate piece of code:
_Ty _Tmp = _Val;   // in case _Val is in sequence

that in fact produces extra temporary object with invocation of that extra ctor/dtor pair.
I have compared the file “vectorâ€


Offline
 Profile  
 
 Post subject:
PostPosted: Wed Sep 22, 2010 2:19 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
Thanks BugHunter!


Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 7 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  

cron