Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Instance method calling a function that deletes the instance

Started by kyc Dec 26, 2005 at 11:07 PM 4 replies 1.1k views
Original Post
kyc
kyc
I was wondering, is there any risk in having an instance of a class calling a function that will delete that instance? In other words: Do methods stay on the function stack even if in the middle of that method's execution its class instance is deleted? I haven't run into any problems yet, but I just want to be sure. Here's what I mean, in C++:

namespace App
{
    void popState()
    {
        // currentState points to an instance of B_State.

        delete currentState;

        currentState = stateStack.top();
    }
}

class B_State : public State
{
public:
    void update()
    {
        App::popState();
    }
}
Skeleton_V@T
Skeleton_V@T
If you manage to manipulate the instance's data after deleting it then my best guess is the program will crash sooner rather than later. You can safely (I mean it is not harmful but definitely a bad habit) execute the rest of that class's method provided that there's no accessing to that class's member data.

If I'm not mistaking, the compiler will supply a hidden this pointer when calling a class's method. So, a class's method is written in C++ as:
class AClass{private:    int m_AMember ;} ;void AClass::AMethod (){    m_AMember = 1 ;}int main (){    AClass AAA ;    AAA.AMethod () ;    return 0 ;}


Can be written equivalently in C as:
struct AClass{    int m_AMember ;} ;void AClass_AMethod (AClass *const p)//You should notive the '_' as in C there's no class definition{    p -> m_AMember = 1 ;}int main (){    AClass AAA ;    AClass_AMethod (&AAA) ; //Equivalent in C++ as: AAA.AMethod ()    return 0 ;}


That said if you delete the memory utilized by an instance then read/write that data, the system may generate an access violation exception. It is similar to the case you access the data of a deleted struct. I hope this helps.
--> The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones <--
kyc
kyc
Hey thanks alot you two, you've been very helpful.

And I'll be sure to read through that entire site :)


LilBudyWizer
LilBudyWizer
Personally, I would make it a class private to another class that manages it, i.e. a state stack. Otherwise you risk changes that should be perfectly legitimate crashing the system like perhaps creating a local variable of that type. There are no problems as long as certain rules are followed and those rules should be hidden/enforced by a class.
Keys to success: Ability, ambition and opportunity.
Teknofreek
Teknofreek
Just off the top of my head, it might work just as well to have the class mark itself as dirty/obsolete/whatever without actually popping it off the stack. Then, on the next frame, you can safely remove and delete any stale states in your stack.

Just an idea...

-John
John

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.