By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,041 Members | 1,734 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,041 IT Pros & Developers. It's quick & easy.

Guessing and destructors

P: n/a
I'm currently working on a roguelike. I've got a global std::vector
list of characters (Yes, I know globals are bad. I'm still learning
Object Oriented programming.) I've also got a handler class to add
characters to that list.

Could exiting the functions of the handler object cause a destructor to
be called for the character list?

Sep 19 '06 #1
Share this Question
Share on Google+
15 Replies


P: n/a
Narf the Mouse wrote:
I'm currently working on a roguelike. I've got a global std::vector
list of characters (Yes, I know globals are bad. I'm still learning
Object Oriented programming.) I've also got a handler class to add
characters to that list.

Could exiting the functions of the handler object cause a destructor to
be called for the character list?
I'm not clear on exactly what you are doing but in general, the
destructor on the vector (being a global) can only be called on the
shutdown of the whole program while elements of the vector may or may
not be constructed/destroyed by adding/removing elements of the vector.
Sep 19 '06 #2

P: n/a

Gianni Mariani wrote:
Narf the Mouse wrote:
I'm currently working on a roguelike. I've got a global std::vector
list of characters (Yes, I know globals are bad. I'm still learning
Object Oriented programming.) I've also got a handler class to add
characters to that list.

Could exiting the functions of the handler object cause a destructor to
be called for the character list?

I'm not clear on exactly what you are doing but in general, the
destructor on the vector (being a global) can only be called on the
shutdown of the whole program while elements of the vector may or may
not be constructed/destroyed by adding/removing elements of the vector.
Well, something is calling the destructor. I put a std::cout into the
destructor to be sure and it's being called while the program is
running. It's not me, either; the destructor is never explicitly called.

Sep 19 '06 #3

P: n/a
Narf the Mouse a écrit :
Gianni Mariani wrote:
>Narf the Mouse wrote:
>>I'm currently working on a roguelike. I've got a global std::vector
list of characters (Yes, I know globals are bad. I'm still learning
Object Oriented programming.) I've also got a handler class to add
characters to that list.

Could exiting the functions of the handler object cause a destructor to
be called for the character list?
I'm not clear on exactly what you are doing but in general, the
destructor on the vector (being a global) can only be called on the
shutdown of the whole program while elements of the vector may or may
not be constructed/destroyed by adding/removing elements of the vector.

Well, something is calling the destructor. I put a std::cout into the
destructor to be sure and it's being called while the program is
running. It's not me, either; the destructor is never explicitly called.
Do you ever erase elements from the vector ? If yes, the destructor will
be called for each erased element.

Pierre
Sep 19 '06 #4

P: n/a
Narf the Mouse wrote:
Well, something is calling the destructor. I put a std::cout into the
destructor to be sure and it's being called while the program is
running. It's not me, either; the destructor is never explicitly called.
Could you post some code (a minimal, compilable version) that
demonstrates your problem?

Regards,
Sumit.
Sep 19 '06 #5

P: n/a
Narf the Mouse wrote:
Gianni Mariani wrote:
>Narf the Mouse wrote:
>>I'm currently working on a roguelike. I've got a global std::vector
list of characters (Yes, I know globals are bad. I'm still learning
Object Oriented programming.) I've also got a handler class to add
characters to that list.

Could exiting the functions of the handler object cause a destructor to
be called for the character list?
I'm not clear on exactly what you are doing but in general, the
destructor on the vector (being a global) can only be called on the
shutdown of the whole program while elements of the vector may or may
not be constructed/destroyed by adding/removing elements of the vector.

Well, something is calling the destructor. I put a std::cout into the
destructor to be sure and it's being called while the program is
running. It's not me, either; the destructor is never explicitly called.
Surly if you assign elements from the vector to local variables
somewhere, they will be destroyed when the scope ends.

Sep 19 '06 #6

P: n/a
Narf the Mouse wrote:
>
Gianni Mariani wrote:
>Narf the Mouse wrote:
I'm currently working on a roguelike. I've got a global std::vector
list of characters (Yes, I know globals are bad. I'm still learning
Object Oriented programming.) I've also got a handler class to add
characters to that list.

Could exiting the functions of the handler object cause a destructor to
be called for the character list?

I'm not clear on exactly what you are doing but in general, the
destructor on the vector (being a global) can only be called on the
shutdown of the whole program while elements of the vector may or may
not be constructed/destroyed by adding/removing elements of the vector.

Well, something is calling the destructor. I put a std::cout into the
destructor to be sure and it's being called while the program is
running.
Did you really put a std::cout in the destructor of the
std::vector< character >
object? Or did you put that line into the destructor of the character class?
Individual elements of the vector may get destroyed every time the vector
resizes.

If you want to check, when the global object is destroyed, you could use
something like this:

#include <vector>
#include <iostream>

struct character {};

struct global_character_vector : public std::vector< character {

global_character_vector ( void )
: std::vector< character ()
{
std::cout << "creatinging: global character vector\n";
}

~global_character_vector ( void ) {
std::cout << "destroying: global character vector\n";
}

} characters;

int main ( void ) {
std::cout << "some activity\n";
}

Remark: the public inheritance from std::vector in this case is a
BAD_HACK(tm) to ensure that you can use the global variable as a drop in
replacement for what you currently have. It can cause some unforeseen
trickyness and you should undo this hack once you confirmed the point of
destruction for the global object.
It's not me, either; the destructor is never explicitly called.

Best

Kai-Uwe Bux
Sep 19 '06 #7

P: n/a

Pierre Barbier de Reuille wrote:
Narf the Mouse a écrit :
Gianni Mariani wrote:
Narf the Mouse wrote:
I'm currently working on a roguelike. I've got a global std::vector
list of characters (Yes, I know globals are bad. I'm still learning
Object Oriented programming.) I've also got a handler class to add
characters to that list.

Could exiting the functions of the handler object cause a destructor to
be called for the character list?
I'm not clear on exactly what you are doing but in general, the
destructor on the vector (being a global) can only be called on the
shutdown of the whole program while elements of the vector may or may
not be constructed/destroyed by adding/removing elements of the vector.
Well, something is calling the destructor. I put a std::cout into the
destructor to be sure and it's being called while the program is
running. It's not me, either; the destructor is never explicitly called.

Do you ever erase elements from the vector ? If yes, the destructor will
be called for each erased element.

Pierre
Sorry for the lateness posting; I found myself unexpectedly burnt out.

Does that apply to pointers as well?

Sep 26 '06 #8

P: n/a

Sumit RAJAN wrote:
Narf the Mouse wrote:
Well, something is calling the destructor. I put a std::cout into the
destructor to be sure and it's being called while the program is
running. It's not me, either; the destructor is never explicitly called.

Could you post some code (a minimal, compilable version) that
demonstrates your problem?

Regards,
Sumit.
Probably not. The thing's an awefull mess right now.

Sep 26 '06 #9

P: n/a

Nils O. Selåsdal wrote:
Narf the Mouse wrote:
Gianni Mariani wrote:
Narf the Mouse wrote:
I'm currently working on a roguelike. I've got a global std::vector
list of characters (Yes, I know globals are bad. I'm still learning
Object Oriented programming.) I've also got a handler class to add
characters to that list.

Could exiting the functions of the handler object cause a destructor to
be called for the character list?
I'm not clear on exactly what you are doing but in general, the
destructor on the vector (being a global) can only be called on the
shutdown of the whole program while elements of the vector may or may
not be constructed/destroyed by adding/removing elements of the vector.
Well, something is calling the destructor. I put a std::cout into the
destructor to be sure and it's being called while the program is
running. It's not me, either; the destructor is never explicitly called.

Surly if you assign elements from the vector to local variables
somewhere, they will be destroyed when the scope ends.
*Bangs head on table*

That's probably it, exactly. I guss I should declare some variables at
class scope, then. Thanks.

Sep 26 '06 #10

P: n/a

Kai-Uwe Bux wrote:
>
Did you really put a std::cout in the destructor of the
std::vector< character >
object? Or did you put that line into the destructor of the character class?
Individual elements of the vector may get destroyed every time the vector
resizes.

If you want to check, when the global object is destroyed, you could use
something like this:

#include <vector>
#include <iostream>

struct character {};

struct global_character_vector : public std::vector< character {

global_character_vector ( void )
: std::vector< character ()
{
std::cout << "creatinging: global character vector\n";
}

~global_character_vector ( void ) {
std::cout << "destroying: global character vector\n";
}

} characters;

int main ( void ) {
std::cout << "some activity\n";
}

Remark: the public inheritance from std::vector in this case is a
BAD_HACK(tm) to ensure that you can use the global variable as a drop in
replacement for what you currently have. It can cause some unforeseen
trickyness and you should undo this hack once you confirmed the point of
destruction for the global object.
It's not me, either; the destructor is never explicitly called.


Best

Kai-Uwe Bux
I put it into the destructor of my ItemHandlerClass.

Sep 26 '06 #11

P: n/a
Narf the Mouse wrote:
>
Kai-Uwe Bux wrote:
>>
Did you really put a std::cout in the destructor of the
std::vector< character >
object? Or did you put that line into the destructor of the character
class? Individual elements of the vector may get destroyed every time the
vector resizes.

If you want to check, when the global object is destroyed, you could use
something like this:

#include <vector>
#include <iostream>

struct character {};

struct global_character_vector : public std::vector< character {

global_character_vector ( void )
: std::vector< character ()
{
std::cout << "creatinging: global character vector\n";
}

~global_character_vector ( void ) {
std::cout << "destroying: global character vector\n";
}

} characters;

int main ( void ) {
std::cout << "some activity\n";
}

Remark: the public inheritance from std::vector in this case is a
BAD_HACK(tm) to ensure that you can use the global variable as a drop in
replacement for what you currently have. It can cause some unforeseen
trickyness and you should undo this hack once you confirmed the point of
destruction for the global object.
It's not me, either; the destructor is never explicitly called.


Best

Kai-Uwe Bux

I put it into the destructor of my ItemHandlerClass.
Well, how is that supposed to detect whether a global object of some
entirely unrelated type is destroyed prematurely?

I would bet you that the global vector of characters is not destroyed before
the end of main. Have you tried the method I suggested to actually
determine the point of destruction for the global object? Well, it does not
really matter: what you said so far is sufficient to tell that there really
is no indication that the global object is destroyed.

With this out of the way, what is the real problem that made you go bug
hunting?
Best

Kai-Uwe Bux
Sep 26 '06 #12

P: n/a

Kai-Uwe Bux wrote:
Narf the Mouse wrote:
I put it into the destructor of my ItemHandlerClass.

Well, how is that supposed to detect whether a global object of some
entirely unrelated type is destroyed prematurely?

I would bet you that the global vector of characters is not destroyed before
the end of main. Have you tried the method I suggested to actually
determine the point of destruction for the global object? Well, it does not
really matter: what you said so far is sufficient to tell that there really
is no indication that the global object is destroyed.

With this out of the way, what is the real problem that made you go bug
hunting?
Best

Kai-Uwe Bux
My ItemHandlerClass contains a vector of ItemClass; that's why I've
been focusing on the ItemHandlerClass. I guess I wasn't clear on that;
sorry. There are no global item vectors.

Sep 28 '06 #13

P: n/a

Narf the Mouse wrote:
Nils O. Selåsdal wrote:

Surly if you assign elements from the vector to local variables
somewhere, they will be destroyed when the scope ends.

*Bangs head on table*

That's probably it, exactly. I guss I should declare some variables at
class scope, then. Thanks.
It doesn't like it when I declare an ItemClass object at class scope.
*Sigh*

Sep 28 '06 #14

P: n/a
Narf the Mouse wrote:
*Bangs head on table*

That's probably it, exactly. I guss I should declare some variables at
class scope, then. Thanks.
A better idea would be to make your objects have a destructor that
works properly. Since you're putting them in a vector, they should
also have a copy-constructor and an assignment-operator that work
properly.

Sep 28 '06 #15

P: n/a

Old Wolf wrote:
Narf the Mouse wrote:
*Bangs head on table*

That's probably it, exactly. I guss I should declare some variables at
class scope, then. Thanks.

A better idea would be to make your objects have a destructor that
works properly. Since you're putting them in a vector, they should
also have a copy-constructor and an assignment-operator that work
properly.
What is a copy-constructor?

Oct 2 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.