18

I have an object which needs to destroy itself.

  • Can it be done?

  • Is the example wrong?

    void Pawn::specialMoves(Coordinate const& from, Coordinate const& to, int passant)
    {
       /*...*/
        m_board->replace(to, new Queen(m_colour));//replace pawn by queen
    }
    
    void Board::replace(Coordinate const &to, Piece* newPiece)
    {
        delete tile[to.x()][to.y()];
        tile[to.x()][to.y()] = newPiece;
    }
    
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
danjjl
  • 432
  • 1
  • 5
  • 16
  • 5
    What object is destroying itself here? FTR, destroying itself would be either `delete this;` or `this->~T();` (with `T` being its type). Both are valid but require lots of care to be used correctly. – R. Martinho Fernandes Mar 25 '12 at 21:49
  • I don't see anything destroying itself in your example. – Oliver Charlesworth Mar 25 '12 at 21:49
  • 1
    Pawn deletes itself. I would imagine once `m_board->replace(to, new Queen(m_colour));` has been called function void Pawn::specialMoves(...)` can not finish ` – danjjl Mar 25 '12 at 21:50
  • 1
    @R.MartinhoFernandes: The question was poorly worded. The pawn calls a method that in turn deletes the pawn. So, in effect, the pawn deletes itself. – Marcelo Cantos Mar 25 '12 at 21:51
  • 2
    It is a *very* common pattern in code that implements memory management with reference counting. Count == 0 is harakiri. – Hans Passant Mar 25 '12 at 22:06

5 Answers5

37

Yes, it's legal to call delete this from inside a member function. But there's very rarely a good reason to do so (especially if you're writing idiomatic C++ where most memory-management tasks should be delegated to containers, smart pointers, etc.).

And you need to be very careful:

  • the suicide object must have been allocated dynamically via new (not new[]).
  • once an object has committed suicide, it is undefined behaviour for it to do anything that relies on its own existence (it can no longer access its own member variables, call its own virtual functions, etc.).
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
11

Here's a good sanity check for performing delete this.

seaotternerd
  • 6,298
  • 2
  • 47
  • 58
John Calsbeek
  • 35,947
  • 7
  • 94
  • 101
  • Curious why the guidelines forbid this with placement new? Do they only mean that you can't `delete this` (since the object wasn't allocated via `new`), or that `this->~T();` is also forbidden? – tchatow Aug 14 '23 at 19:15
2

Yes, it should work. Even delete this; is allowed.

But the code calling specialMoves() could be in for a nasty surprise.

H H
  • 263,252
  • 30
  • 330
  • 514
1

Q: Can an object destroy itself?

A: Sure. "delete this" is a popular idiom in COM/ActiveX

As far as your algorithm, I'd suggest:

  • a "board" object has "tiles". Perhaps just a simple 2-D array.

  • You start out with n "pieces"

  • Some controller (perhaps a "game" object), moves a "piece" with respect to a "tile".

  • Each "tile" has a reference to 0 or 1 "pieces"

I'm not sure I see any reason to create or delete anything on a per-move basis.

IMHO...

paulsm4
  • 114,292
  • 17
  • 138
  • 190
0

As long as you don't access member variables or the this pointer after the call to destroy the object, you should be fine. Since it doesn't appear you're doing either of these things, the example should work.

tmpearce
  • 12,523
  • 4
  • 42
  • 60