Commit 6de5b422 authored by Sylvain Berfini's avatar Sylvain Berfini 🎩

Fix circular dependency in shared_ptr to fix memory leak

parent cd86ee7d
......@@ -163,7 +163,7 @@ public:
**/
BELR_PUBLIC Grammar(const string &name);
virtual ~Grammar() { }
~Grammar();
/**
* Include another grammar into this grammar.
......@@ -229,6 +229,7 @@ private:
void assignRule(const string &name, const shared_ptr<Recognizer> &rule);
void _extendRule(const string &name, const shared_ptr<Recognizer> &rule);
map<string,shared_ptr<Recognizer>> mRules;
list<shared_ptr<RecognizerPointer>> mRecognizerPointers;
string mName;
};
......
......@@ -381,6 +381,12 @@ Grammar::Grammar(const string& name) : mName(name){
}
Grammar::~Grammar() {
for(auto it = mRecognizerPointers.begin(); it != mRecognizerPointers.end(); ++it) {
shared_ptr<RecognizerPointer> pointer = dynamic_pointer_cast<RecognizerPointer>(*it);
pointer->setPointed(NULL);
}
}
void Grammar::assignRule(const string &argname, const shared_ptr<Recognizer> &rule){
string name=tolower(argname);
......@@ -440,10 +446,12 @@ shared_ptr<Recognizer> Grammar::getRule(const string &argname){
}
return ret;
}else{/*the rule doesn't exist yet: return a pointer*/
ret=make_shared<RecognizerPointer>();
shared_ptr<RecognizerPointer> recognizerPointer = make_shared<RecognizerPointer>();
ret=recognizerPointer;
string name=tolower(argname);
ret->setName(string("@")+name);
mRules[name]=ret;
mRecognizerPointers.push_back(recognizerPointer);
}
return ret;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment