3
class AutoSomething
{
public:
    AutoSomething(Object& ob)
        : object_(object)
    {}

    ~AutoSomething()
    {
        object_.some_callback();
    }

private:
    Object& object_;
};

.........

void Object::some_function()
{
    AutoSomething some(*this);

    some_function_which_may_throw_exception();
}

The question is - will the state of Object be OK when the destructor of AutoSomething will be called?

user1289
  • 1,251
  • 2
  • 13
  • 25
  • What do you mean the state is OK? It's true that the dtor of `AutoSomething` will be called and then `Object::some_callback()` will be called. – songyuanyao Jul 01 '16 at 05:39
  • If you call a function in your destructor that throws your program will terminate. Typically you don't control what callback functions do so I would avoid calling calbacks in a destructor. – Galik Jul 01 '16 at 05:42
  • Yes they'll be called, but I doubted that Object could be corrupted at that time. – user1289 Jul 01 '16 at 05:43
  • @user1289 It won't be corrupted with the invoking of the dtor of `AutoSomething`, `Object` is just saved in `AutoSomething` as a reference. – songyuanyao Jul 01 '16 at 05:44
  • @Galik no it's simple callback. What else you suggest? there are many cases where function can return, so I would call that callback in all cases? What if I miss some exception? – user1289 Jul 01 '16 at 05:45
  • @songyuanyao Yes probably that's true, but you are suspecting everything when program works unpredictably. – user1289 Jul 01 '16 at 05:46
  • @user1289 Use debugger. You can confirm whether it's corrupted or not clearly. – songyuanyao Jul 01 '16 at 05:48
  • it's hard to use debugger when program crushes once in 20 run. – user1289 Jul 01 '16 at 05:48
  • Stack unwinding is exactly the situation where RAII works best. The state of the object when the destructor is called is whatever state you left it in just beforehand. Unclear what you're asking. Probable XY problem. – user207421 Jul 01 '16 at 06:00
  • @Galik: 'if you call a function in your destructor that throws' *and you don't catch it* 'your program terminates'. – lorro Jul 01 '16 at 07:21
  • @lorro Yes true. I should have said that. But destructors are not good places to handle exceptions. So even if you catch any exceptions, its not a great place to call callbacks from if it can be avoided. – Galik Jul 01 '16 at 07:27

1 Answers1

6

Stack unwinding is the situtation for which RAII was invented in the first place. So it is most certainly the proper tool for that.

In your particular case, there is no reason why the code should behave incorrectly. The only problem could arise if some_callback relies on an internal invariant of Object which is not maintained when some_function_which_may_throw_exception actually throws, but that would a problem of the particular code and has nothing to do with C++ itself.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • Thanks. Yes, you are right. The reason was my carelessness, I wonder how I wrote `AutoSomething(*this);` instead of `AutoSomething auto(*this)`. – user1289 Jul 01 '16 at 07:12