Menu

#278 Memory leak for AbsEntity

Version 5.10
open
nobody
None
5
2015-04-02
2015-04-02
unicode
No

Memory leak for AbsEntity

void AbsEntity::SetValue(string value) {
if ( this->value.empty() ){
this->value.push_back(new StringEntityValue(value)); //memory allocated
}else{
this->value.front()->SetValue(value);
}
}

Destructor does nothing, so memory leaked

Suggestion:
(1)relase memory in Destructor
(2)Because of the absence of the COPY CONSTRUCTOR, the default CONSTRUCTOR will be used automatically, then the members(include 'vaule' member) will be shallow-copied. So, if a Entity Object is copied, the memory will be released multi times, error will occur.

For example:

StringVector AbsFileFinder::GetPaths(ObjectEntity path, BehaviorVector behaviors) {
auto_ptr<stringvector> pathsFound(new StringVector());
// make a copy of the path so I can switch its operation without
// affecting the original.
ObjectEntity tmpPath(</stringvector>
path); //default consctrutor will be called!!!
...

**Code Description as following: **

AbsEntity::AbsEntity(AbsEntity &a){
this->name = a.name;
this->value = a.value;
this->datatype = a.datatype;
this->varCheck = a.varCheck;
this->operation = a.operation;
this->varRef = a.varRef;
this->nil = a.nil;

this.shallowCopy = true; //Added, the member (type boolean) added as a flag

}

AbsEntity::AbsEntity(string name, string value, OvalEnum::Datatype datatype, OvalEnum::Operation operation, AbsVariable* varRef, OvalEnum::Check varCheck, bool nil) {
..
this->shallowCopy = false;
}

AbsEntity::~AbsEntity(){
if (!shallowCopy){
//iterator the value vector, relasing the memeory
...
}
}

Discussion


Log in to post a comment.

MongoDB Logo MongoDB