C++ Vector of Object Pointers- Destructor not working

Hi guys,

I have a vector that is holding pointers to 7 objects, but my destructor does a single delete and stops. Even live debugging it just gets stuck at the first delete.

Vector of Object pointers: std::vector<Comparable*> compareVec;

1
2
3
4
5
6
7
  inline HighScore::~HighScore(){
   for(std::vector<Comparable*>::const_iterator it = compareVec.begin(); it != compareVec.end(); ++it){
    std::cout << "Comparable Destructor" << std::endl;
    delete *it;
  }
   std::cout << "Object pointers deleted." << std::endl;
}



ty

Edit: Mind you I'm new and would like to refrain from using smart pointers, boost, etc. I need to learn the basics first.
Last edited on
What do you mean by "stops"? Does it crash? trap? exit? hang?
Do you have a copy constructor and copy assignment operator for HighScore? How are the elements allocated? If there is an inheritance tree and Comparable is at the top, does it have a virtual destructor?
Hi,

The destructor just hangs, so when I click f5 it will cout comparable destructor, but thats it... doesnt exit, doesnt cout object pointers deleted or anything.


No copy constructor or anything special, I just make the objects and push them in as pointers. Like so:

compareVec.push_back(&object);

I simply push_back the objects:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  HighScore c1("Lawrence", 2300);
  HighScore c2("Mark", 2750);
  HighScore c3("Chris", 2105);
  HighScore c4("Bob", 3120);
  HighScore c5("Kyle", 2300);
  HighScore c6("Trevor", 1785);
  HighScore c7("Josh", 2850);

  HighScore oManager;

  oManager.pushObjectVector(c1);
  oManager.pushObjectVector(c2);
  oManager.pushObjectVector(c3);
  oManager.pushObjectVector(c4);
  oManager.pushObjectVector(c5);
  oManager.pushObjectVector(c6);
  oManager.pushObjectVector(c7);


EDIT: I'm pretty sure Im doing the virtual destructor wrong: (but shouldnt matter because it is indeed entering HIGHSCORE destructor.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class Comparable{
public:
  virtual int getScore() = 0;

  virtual std::string getName() = 0;

  virtual void PrintObjectList() = 0;

  virtual void print() = 0;

  virtual ~Comparable();

};




class HighScore : Comparable{
private:
  std::vector<Comparable*> compareVec;
  std::string firstName;
  int score;

public:

  inline HighScore();

  inline HighScore(std::string name, int score);

  inline ~HighScore();

  inline bool operator<(HighScore* Obj2);

  int getScore();

  std::string getName();

  void compare();

  void pushObjectVector(HighScore& object);

  void print();

  void PrintObjectList();

  void sort();
};
Last edited on
(1) ordinary delete
Deallocates the memory block pointed by ptr (if not null), releasing the storage space previously allocated to it by a call to operator new and rendering that pointer location invalid.
From here: http://www.cplusplus.com/reference/new/operator%20delete/

Your objects are allocated on the stack.
Lets recap: HighScore is a Comparable. HighScore has a list of Comparables. On destruction, a HighScore object destroys the other Comparables that it has in the list.

Who are those other Comparables? Where did they come from? What side-effects does their destruction cause?

Most importantly, what does the pushObjectVector do?


Edit: My guess is that your error is:
1
2
3
int a;
int * b = &a;
delete b;
Last edited on
So unless I use keyword "new" the objects will be on the stack?

This project is supposed to sort the objects inside the vector based on their values, first by the score then if both obj 1 and 2 have the same score then sort by name.

I'm still not sure how to fix it, since vectors clear whats inside them automatically but the pointers remain.
So unless I use keyword "new" the objects will be on the stack?

We don't have the context of your object definitions (and C++ doesn't have any concept of a stack in this sense.) They may be objects with automatic storage duration. What they won't be is objects whose addresses you may feed to delete.

I'm still not sure how to fix it, since vectors clear whats inside them automatically but the pointers remain.

Simple enough: don't delete them. But, why should a HighScore contain a vector of Comparable* and why does Comparable have such a bloated interface?
My object holds a name and a score, not sure what u mean by def.

This is my first project to practice polymorphism and inheritance, not expecting much from it. I just needed a way to test new concepts to me.
edit:
why are you dereferencing the iterator and deleting objects that have no memory allocated to them?

 
delete *it;


your objects are not allocated on the heap.

what you really have is this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
compareVec[0] = c1("one", 33);

//what you need is

compareVec[0] =  new c1("one", 33);

//or
 HighScore *c1 = new HighScore("Lawrence", 2300);

 oManager.pushObjectVector(c1);

//why should oManager delete c1?
//it is not instantiated inside the oManager instance


edit: I confused myself
Last edited on
Gkneeus wrote:
what you really have is this

1
2
3
4
5
compareVec[0] = c1("one", 33);

//what you need is

compareVec[0] =  new c1("one", 33);


No, that's not what he really has and it's not clear that's what he needs. c1 is an object, not a class. Were you to use the name of the class, line 1 would still be in error. Were you to additionally take the address of the temporary created thusly, you still would not be correct.
Last edited on
@Cire,

I got confused after looking at his code, what I meant to say was it looks like he is passing in the C1 objects to the vector in the oManager object and trying to free the memory of the C1 objects in the oManger destructor.
Last edited on
That makes alot more sense , sry I always confuse allocated data with regular pointers.

If those were in fact allocated, would the syntax be acceptable?
no, c1, c2, ect.... should not be deleted inside the oManager instance of the highscore class because they were not allocated memory inside that oManager constructor, so it should not be deletd in that destructor. if you allocate memory for them in Main they should be deleted in Main.
Last edited on
no, c1, c2, ect.... should not be deleted inside the oManager instance of the highscore class because they were not allocated memory inside that oManager constructor, so it should not be deletd in that destructor. if you allocate memory for them in Main they should be deleted in Main.


This is patently untrue. The "owner" of the object is responsible for determining its lifetime, regardless of where it began and it is, in fact, common practice to begin a lifetime prior to the "owner" gaining possession of said object.
This is patently untrue. The "owner" of the object is responsible for determining its lifetime, regardless of where it began and it is, in fact, common practice to begin a lifetime prior to the "owner" gaining possession of said object.

Yes, you are right but, in his case wouldn't it be better not to? The code looks like its all over the place, he said he want's to understand the basics first. I am guessing he has a vector of *Comparable because he wants to use polymorphism. I was just saying that he should clearly separate the code he is using and get reorganized because it looks like he is trying to learn the basics by using pointers for dynamically allocated memory, polymorphism, and passing by reference in one project. I think that it is too overwhelming for someone who needs to learn the basics first. I would say before fixing his destructor go and put comments in your code before racing back and forth to fix a bug because you will be trying to read code at 100 mph and debug at the same time while flipping back and forth.
Registered users can post here. Sign in or register to post.