mardi 27 mars 2018

Is there a term for when two methods of the same object are on the stack, with a method of a different object in between?

I'm working on a best practices guide, and there's a particular (anti)pattern I want to mention; unfortunately, I don't know the official name for it. Here's an example:

Suppose we have two objects, master and slave, whose basic interface looks like this:

class Master {
    Slave slave;

    public void startSlave() {
        slave.doWork();
    }

    public void onSlaveFinished() {
        slave.dispose();
    }
}

class Slave {
    Master master;

    public void doWork() {
        // ...do some work

        master.onSlaveFinished();

        // ...maybe do something here?
    }

    public void dispose() {
        // dispose of all resources held by this object
    }
}

In this arrangement, when master.startSlave() is called, the stack will end up looking like this:

slave.dispose()
master.onSlaveFinished()
slave.doWork()
master.startSlave()

The problem here is that when slave.dispose() finishes, slave.doWork() is still running, and it might try to do something with the resources that slave no longer holds.

So, my question would be, is there a name for when the stack ends up looking like this? Specifically, I mean that object A calls a method of object B, which ends up calling another method of object A, which can then leave object A in an inconsistent state.

I've tried googling a few terms I could think of ("stack overreach", "stack interleaving", "stack sandwich") but none of those seem to be recognized.

Aucun commentaire:

Enregistrer un commentaire