Guessing and destructors

N

Narf the Mouse

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?
 
G

Gianni Mariani

Narf said:
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.
 
N

Narf the Mouse

Gianni said:
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.
 
P

Pierre Barbier de Reuille

Narf the Mouse a écrit :
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
 
S

Sumit RAJAN

Narf said:
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.
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Narf said:
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.
 
K

Kai-Uwe Bux

Narf said:
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
 
N

Narf the Mouse

Pierre said:
Narf the Mouse a écrit :

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?
 
N

Narf the Mouse

Sumit said:
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.
 
N

Narf the Mouse

Nils said:
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.
 
N

Narf the Mouse

Kai-Uwe Bux said:
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.



Best

Kai-Uwe Bux

I put it into the destructor of my ItemHandlerClass.
 
K

Kai-Uwe Bux

Narf said:
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
 
N

Narf the Mouse

Kai-Uwe Bux said:
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.
 
N

Narf the Mouse

Narf said:
*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*
 
O

Old Wolf

Narf said:
*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.
 
N

Narf the Mouse

Old said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top