0

I'm trying to add pointers to a vector in C++. As such:

          Puzzle * puzzleStart = new Puzzle();

          std::vector<Puzzle*> OPEN;

          OPEN.push_back(puzzleStart);

The first time a pointer is pushed, there is no problem. The second time, it causes a crash. I'm guessing the issue is the size of the vector, but I don't understand why. Is there anything more to this?

Update: You are right, the problem is elsewhere, I just realized that it occurs while I free the vector of pointer. There is another issue, if the vector contains dupplicates of pointers I think.

   if (OPEN.size()!=0){
       for (int i = 0; i < OPEN.size(); ++i) {
      delete OPEN[i]; // Calls ~object and deallocates *tmp[i]
       }
      OPEN.clear();
       } 

How do i make sure that it doesn't try to erase allready deleted pointers?

  • 1
    Adding a simple pointer to a vector of pointers would not crash the program. Post the crash with error message and add code until your second crash happens. Maybe some vector manipulation you shouldn't do ? The current code in your question does nothing wrong (though you might want to use smart pointers, if possible). – Blacktempel Feb 16 '16 at 21:00

3 Answers3

0

You are right, the problem is elsewhere, I just realized that it occurs while I free the vector of pointer. There is another issue, if the vector contains dupplicates of pointers I think.

       if (OPEN.size()!=0){
           for (int i = 0; i < OPEN.size(); ++i) {
          delete OPEN[i]; // Calls ~object and deallocates *tmp[i]
           }
          OPEN.clear();
           } 

How do i make sure that it doesn't try to erase allready deleted pointers?

0

If the problem is duplication of pointers, you should consider a container that does not allow duplication, such as a set. E.g.:

std::set<Puzzle*> s;
Puzzle *puzz = new Puzzle();
auto insert_result = s.insert(puzz);

if(!insert_result.second)
{
    std::cout << "\"puzz\" was a duplication. No insertion made.\n";
}

// More items inserted into s, and used, etc.

for(auto p : s)
    delete p;

s.clear();
Michael Cornelius
  • 1,516
  • 1
  • 11
  • 11
  • Yes, though the problem is not a dupplicate within one vector, but shared dupplicates between two vectors, How is it done in that case? – François Lalonde Feb 16 '16 at 21:51
  • There should always be exactly one "owner" of an allocated resource like a pointer. One of your vectors should be the sole owner of all the pointers, and should delete them all before being cleared. The other vector should never delete the pointers it contains. You must also ensure you never dereference the pointers after you delete them. If your vectors contain intersecting sets of pointers -- that is, each has some pointers contained by the other, and neither is a clear owner of all the pointers -- you have a problem and need to reconsider your design. – Michael Cornelius Feb 17 '16 at 15:32
0

When you delete the pointer, set it to nullptr. Deleting a null pointer does not cause a crash.

Guest
  • 11
  • 1