Friday, 23 November 2018

c++ - Where should I call delete in this case?





I am programming a finite state machine using the state pattern taken from here:



State Pattern



and I have implemented it here:



My Machine Github



Recalling what i did imagine you have this situation:




//MAIN MACHINE
class machine{

public:

void handle(){

_state->handle();

}

void setStatePtr(AbstrState *state){

_state = state;
}

private:
AbstrState* _state;

};


//BASE STATE
class AbstrState {

public:

AbstrState(machine* m){
_context = m;
}
virtual void handle() = 0;


protected:

machine *_context;

};

//ACTUAL STATES
class ONState : AbstrState{
public:


ONState(machine *m) : AbstrState(m) {}

void handle(){

if(BUTTON_PRESSED){

_context->setStatePtr( new OFFState(_context));

}
}



};

class OFFState : AbstrState{
public:

OFFState(machine *m) : AbstrState(m) {}

void handle(){


if(BUTTON_PRESSED){

_context->setStatePtr( new ONState(_context) );

}
}

};



The machine is initialized with setStatePtr(new OFFState(&machine))



My question rises here: since I am using the new operator, I should call delete somewhere but where? I tried in setStatePtr function doing:



void setStatePtr(.. s){
delete _state;
_state = s;
}



but it doesn't work.



I am not defining any pointer p* = new p(); so I dont know where to call any delete. My question is more thorethical so please don't suggest other implementation or usagese because I will still have the same doubt :)



When i do foo(new something) where do i call delete?



Thank in advance,



Andrea



Answer



Old way is to implement that method this way:



void setStatePtr(AbstrState *state){
if( _state != state ) {
delete _state;
_state = state;
}
}



but proper way is to use smart pointers:



class machine{
public:

void setStatePtr( std::unique_ptr state){

_state = std::move( state );
}


private:
std::unique_ptr _state;
};


this way you have 2 benefits:




  • resource managed automatically and destroyed when necessary


  • your method signature clearly shows that it is going to take ownership of passed object, when you pass raw pointer it is unclear and you have to explain that in documentation, which is error prone


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...