Skip to content

Issue when deleting StateMachine pointer #5

@yles9056

Description

@yles9056

Hi,
I tried to create multiple StateMachines using new operator, but when I delete one of them, the other StateMachines throw exception when excecuting state->InvokeStateAction() in StateMachine::StateEngine()

The error reads:
Exception thrown: read access violation.
state->**** was 0xFFFFFFFFFFFFFFFF.

Here's how I test it:

TestStateMachine* testStateMachine1 = new TestStateMachine();
TestStateMachine* testStateMachine2 = new TestStateMachine();

testStateMachine1->open();
testStateMachine2->open();

testStateMachine1->close();
delete testStateMachine1;
testStateMachine1 = nullptr;

testStateMachine2->close(); // Throws error when excecuting InvokeStateAction() inside StateEngine()
delete testStateMachine2;
testStateMachine2 = nullptr;

TestStateMachine is defined as below:

// TestStateMachine.h
#include <StateMachine.h>

class TestStateMachine : public StateMachine {
public:
    TestStateMachine();

    void open();
    void close();

protected:
    enum States {
        ST_CLOSE,
        ST_OPEN,
        ST_MAX_STATES
    };

    STATE_DECLARE(TestStateMachine, Close, NoEventData)
    STATE_DECLARE(TestStateMachine, Open, NoEventData)

    BEGIN_STATE_MAP
        STATE_MAP_ENTRY(&Close)
        STATE_MAP_ENTRY(&Open)
    END_STATE_MAP
};
// TestStateMachine.cpp
#include "TestStateMachine.h"
#include <iostream>

TestStateMachine::TestStateMachine() : StateMachine(ST_MAX_STATES) {}

void TestStateMachine::open() {
    BEGIN_TRANSITION_MAP
        TRANSITION_MAP_ENTRY(ST_OPEN)
        TRANSITION_MAP_ENTRY(EVENT_IGNORED)
    END_TRANSITION_MAP(NULL)
}

void TestStateMachine::close() {
    BEGIN_TRANSITION_MAP
        TRANSITION_MAP_ENTRY(EVENT_IGNORED)
        TRANSITION_MAP_ENTRY(ST_CLOSE)
    END_TRANSITION_MAP(NULL)
}

STATE_DEFINE(TestStateMachine, Open, NoEventData) {
    std::cout << "TestStateMachine::Open";
}

STATE_DEFINE(TestStateMachine, Close, NoEventData) {
    std::cout << "TestStateMachine::Close";
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions