Good afternoon , i'm starting a new Arduino Project 1.6.10 IDE ver. but i'm encountering some problems of memory leak when i use a class based structure.
I post my code first and then i'll point the place when the memory leak seems to appear.
mainSketchFile.
#include <Ethernet.h>
#include <MemoryFree.h>
#include "Constants.h"
#include "State.h"
StateFactory CurrentStateFactory;
void setup() {
pinMode(BUZZER,OUTPUT);
Serial.begin(9600);
Serial.println("START");
delay(1000);
}
void loop() {
Serial.print(F("Free RAM = "));
Serial.println(freeMemory(), DEC); // print how much RAM is available.
CurrentStateFactory.changeStatus(1);
Serial.println(CurrentStateFactory.getCurrentState()->getNumber());
CurrentStateFactory.changeStatus(2);
Serial.println(CurrentStateFactory.getCurrentState()->getNumber());
}
The problem seems to be in State.h i marked the point in the comments
#ifndef State_h
#define State_h
/////////////////// STATE/////////////////////////
class MachineState{
public:
virtual int getNumber();
protected:
};
/////////////////////ACTIVE FULL/////////////////////////////////
class ActiveFull : public MachineState
{
public:
ActiveFull();
virtual int getNumber();
private:
String statusName; //<----- PROBLRM SEEMS TO BE HERE WHEN COMMENTED NO MEMORY LEAK APPEN
int number;
};
ActiveFull::ActiveFull(){
this->number=1;
};
int ActiveFull::getNumber(){
return this->number;
}
////////////////////////////// ACTIVE EMPTY ////////////////////
class ActiveEmpty : public MachineState
{
public:
ActiveEmpty();
virtual int getNumber();
protected:
String statusName;//<----- PROBLRM SEEMS TO BE HERE WHEN COMMENTED NO MEMORY LEAK APPEN
int number;
};
ActiveEmpty::ActiveEmpty(){
this->number=2;
};
int ActiveEmpty::getNumber(){
return this->number;
}
//////////////////FACTORY/////////////////////////////
class StateFactory{
private:
MachineState *currentState;
public:
StateFactory();
void *changeStatus(int choice); // factory
MachineState *getCurrentState();
};
StateFactory::StateFactory(){
MachineState *var1=new ActiveFull();
this->currentState=var1;
}
MachineState *StateFactory::getCurrentState(){
return this->currentState;
}
void *StateFactory::changeStatus(int choice)
{
delete this->currentState; // to prevent memory leak
if (choice == 1){
MachineState *var1=new ActiveFull();
this->currentState=var1;
}
else if (choice == 2){
MachineState *var1=new ActiveEmpty;
this->currentState=var1;
}
else{
MachineState *var1=new ActiveEmpty;
this->currentState=var1;
}
}
#endif
i use the library to track the memory usage and this is the output of the sketch:
No memory Leak (String statusName commented)
Free RAM = 7897
1
2
Free RAM = 7897
1
2
Free RAM = 7897
1
2
Free RAM = 7897
1
2
Free RAM = 7897
1
2
Free RAM = 7897
1
2
Free RAM = 7897
1
2
Free RAM = 7897
1
2
Free RAM = 7897
1
2
Memory leak when property String statusName is uncommented
Free RAM = 6567
1
2
Free RAM = 6559
1
2
Free RAM = 6551
1
2
Free RAM = 6543
1
2
Free RAM = 6535
1
2
Free RAM = 6527
1
2
Thanks in advise for your Time. Hope you can help me.
it seems like a destructor's problem,
I post a an implementation based on your code ..
This is my result with your main: