Gui: [skip ci] fix memory leak in SelectionFilter

This commit is contained in:
wmayer
2021-02-27 18:17:52 +01:00
parent bd755b654b
commit 82354979c9
4 changed files with 31 additions and 31 deletions

View File

@@ -137,13 +137,13 @@ bool SelectionFilterGatePython::allow(App::Document*, App::DocumentObject* obj,
// ----------------------------------------------------------------------------
SelectionFilter::SelectionFilter(const char* filter)
: Ast(0)
: Ast(nullptr)
{
setFilter(filter);
}
SelectionFilter::SelectionFilter(const std::string& filter)
: Ast(0)
: Ast(nullptr)
{
setFilter(filter.c_str());
}
@@ -151,8 +151,7 @@ SelectionFilter::SelectionFilter(const std::string& filter)
void SelectionFilter::setFilter(const char* filter)
{
if (!filter || filter[0] == 0) {
delete Ast;
Ast = 0;
Ast.reset();
Filter.clear();
}
else {
@@ -172,43 +171,41 @@ bool SelectionFilter::match(void)
return false;
Result.clear();
for (std::vector< Node_Object *>::iterator it= Ast->Objects.begin();it!=Ast->Objects.end();++it) {
int min;
int max;
for (const auto& it : Ast->Objects) {
std::size_t min = 1;
std::size_t max = 1;
if ((*it)->Slice) {
min = (*it)->Slice->Min;
max = (*it)->Slice->Max;
}
else {
min = 1;
max = 1;
if (it->Slice) {
min = it->Slice->Min;
max = it->Slice->Max;
}
std::vector<Gui::SelectionObject> temp = Gui::Selection().getSelectionEx(0,(*it)->ObjectType);
std::vector<Gui::SelectionObject> temp = Gui::Selection().getSelectionEx(nullptr, it->ObjectType);
// test if subnames present
if ((*it)->SubName.empty()) {
if (it->SubName.empty()) {
// if no subnames the count of the object get tested
if ((int)temp.size()<min || (int)temp.size()>max)
if (temp.size() < min || temp.size() > max)
return false;
}
else {
// if subnames present count all subs over the selected object of type
int subCount=0;
std::size_t subCount = 0;
for (std::vector<Gui::SelectionObject>::const_iterator it2=temp.begin();it2!=temp.end();++it2) {
const std::vector<std::string>& subNames = it2->getSubNames();
if (subNames.empty())
return false;
for (std::vector<std::string>::const_iterator it3=subNames.begin();it3!=subNames.end();++it3) {
if (it3->find((*it)->SubName) != 0)
if (it3->find(it->SubName) != 0)
return false;
}
subCount += subNames.size();
}
if (subCount<min || subCount>max)
if (subCount < min || subCount > max)
return false;
}
Result.push_back(temp);
}
return true;
@@ -219,13 +216,13 @@ bool SelectionFilter::test(App::DocumentObject*pObj, const char*sSubName)
if (!Ast)
return false;
for (std::vector< Node_Object *>::iterator it= Ast->Objects.begin();it!=Ast->Objects.end();++it) {
if (pObj->getTypeId().isDerivedFrom((*it)->ObjectType)) {
for (const auto& it : Ast->Objects) {
if (pObj->getTypeId().isDerivedFrom(it->ObjectType)) {
if (!sSubName)
return true;
if ((*it)->SubName.empty())
if (it->SubName.empty())
return true;
if (std::string(sSubName).find((*it)->SubName) == 0)
if (std::string(sSubName).find(it->SubName) == 0)
return true;
}
}
@@ -401,7 +398,7 @@ bool SelectionFilter::parse(void)
ActFilter = this;
/*int my_parse_result =*/ SelectionParser::yyparse();
ActFilter = 0;
Ast = TopBlock;
Ast.reset(TopBlock);
TopBlock = 0;
SelectionParser::SelectionFilter_delete_buffer (my_string_buffer);

View File

@@ -24,6 +24,7 @@
#ifndef GUI_SelectionFilter_H
#define GUI_SelectionFilter_H
#include <memory>
#include <string>
#include <CXX/Extensions.hxx>
#include "Selection.h"
@@ -87,8 +88,7 @@ protected:
std::string Errors;
bool parse(void);
Node_Block *Ast;
std::shared_ptr<Node_Block> Ast;
};
/** Filter object for the SelectionSengleton
@@ -220,11 +220,14 @@ struct Node_Object
Node_Slice *Slice;
std::string SubName;
};
typedef std::shared_ptr<Node_Object> Node_ObjectPtr;
struct Node_Block
{
Node_Block(Node_Object* obj){Objects.push_back(obj);}
std::vector< Node_Object *> Objects;
Node_Block(Node_Object* obj){
Objects.emplace_back(obj);
}
std::vector<Node_ObjectPtr> Objects;
};

View File

@@ -1408,7 +1408,7 @@ yyreduce:
/* Line 1464 of yacc.c */
#line 53 "SelectionFilter.y"
{ (yyval.block) = (yyvsp[(1) - (2)].block) ; (yyval.block)->Objects.push_back((yyvsp[(2) - (2)].object)); ;}
{ (yyval.block) = (yyvsp[(1) - (2)].block) ; (yyval.block)->Objects.emplace_back((yyvsp[(2) - (2)].object)); ;}
break;
case 13:

View File

@@ -69,7 +69,7 @@ count : { $$ = 0 }
matchline : type subname count { $$ = new Node_Object($1,$2,$3) }
matchlines : matchline { $$ = new Node_Block($1); }
| matchlines matchline { $$ = $1 ; $$->Objects.push_back($2); }
| matchlines matchline { $$ = $1 ; $$->Objects.emplace_back($2); }
block : matchlines { $$ = $1 }