c++ what happens if callee deletes caller?
is the behaviour in a c++ app defined if a callee function deletes the object in which the caller (member) function is defined? will the rest of the caller function body still b开发者_运维百科e executed? will it run correctly if it doesn't access any member variables?
I just ask because I found this case in my application (the result of some juggling with member function pointers) and I was surprised why it doesn't make my app crash.
Yes, that is the expected behavior. As long as the code does not access any non-static member objects or functions, there is no reason why it can't keep running.
A notable exception to "yes the rest of the caller will run" is Win32's FreeLibraryAndExitThread
, which REALLY deletes the caller, stack space, code, and all.
The code is still on the stack even though the destructor was called. You can't depend on the fact that any free'd memory won't be overwritten in the meanwhile, though. If it isn't overwritten, however, and the destructor doesn't otherwise overwrite anything critical, things would go along smoothly.
精彩评论