Thread: Vector Class calling deconstructer

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    8

    Vector Class calling deconstructer

    hi, im writing a gui for my 2d engine(sdl powered) and hit snag, it apears that each time i add a form class to the vector the deconstructer of every item in the vector is called,

    why is this? and more importantly is it my fault

    this is the method that creates a new form, the name changes are just there so i could log which deconstructers are being called.

    Code:
    void PGUI::AddForm(std::string name, int x, int y, int width, int height){
    	if(Forms.size()<Forms.max_size()){
    		PGUI_Form newform(name,x,y,width,height);
    		newform.name = parse("%s in vector",(char *)name.c_str());
    		Forms.push_back(newform);
    		newform.name = parse("%s not in vector",(char *)name.c_str());
    	}
    }
    aplicable Log entries:

    Initialising Window form1
    PGUI_Form: Deinitialising form1 in vector
    PGUI_Form: Deinitialising form1 not in vector
    Initialising Window form2
    PGUI_Form: Deinitialising form1 in vector
    PGUI_Form: Deinitialising form2 in vector
    PGUI_Form: Deinitialising form2 not in vector
    Initialising Window form3
    PGUI_Form: Deinitialising form1 in vector
    PGUI_Form: Deinitialising form2 in vector
    PGUI_Form: Deinitialising form3 in vector
    PGUI_Form: Deinitialising form3 not in vector

    Prophecy Engine: Shutting Down
    PGUI_Form: Deinitialising form1 in vector
    PGUI_Form: Deinitialising form2 in vector
    PGUI_Form: Deinitialising form3 in vector

    Any help would be apretiated,

    thanks, p3p

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    When you call push_back on a vector, a copy of the object is added to the vector. So the variable newform goes out of scope and is destructed at the end of the if block but there is a second instance stored in the vector.

    If your PGUI_Form class has valid copy constructor and copy assignment operator, then it should work ok and you shouldn't worry about the extra destruction because there is an extra copy construction taking place as well.

    If it does not have valid copy methods, or the default ones won't work properly because you have some dyamically allocated memory or other handles that are destroyed in the destructor, or if it is expensive to copy, then you can store a shared_ptr or regular pointer in the vector. If you store a regular pointer you have to manage the memory yourslef with new and delete.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    thanks for the explanation daved, now to go see if i can fix it , my font class releases the font when the Form class is deconstructed, so bad things happen

    p3p

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Either write a valid copy constructor and copy assignment operator that duplicates the font handle, or make those functions private so that the compiler won't let you accidentally make a copy and release the font early. If you make them private, you'll have to store some sort of pointer (but not std::auto_ptr... it does not copy correctly either).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack issues when calling a COM library
    By notnot in forum C Programming
    Replies: 3
    Last Post: 06-01-2009, 02:12 AM
  2. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  3. calling default instead of argument constructor, why?!
    By cppn00b in forum C++ Programming
    Replies: 6
    Last Post: 01-30-2005, 04:24 AM
  4. calling conventions
    By Micko in forum C Programming
    Replies: 2
    Last Post: 07-18-2004, 09:13 AM
  5. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM