Fix a gross memory leak: when switching to a new file, either after

File -> New or File -> Open, I freed the Group structures
themselves, but none of their children.

[git-p4: depot-paths = "//depot/solvespace/": change = 2120]
This commit is contained in:
Jonathan Westhues 2010-02-28 11:23:01 -08:00
parent 7b10381b80
commit a11010c593
6 changed files with 51 additions and 35 deletions

View File

@ -6,6 +6,29 @@ static int StrStartsWith(char *str, char *start) {
return memcmp(str, start, strlen(start)) == 0;
}
//-----------------------------------------------------------------------------
// Clear and free all the dynamic memory associated with our currently-loaded
// sketch. This does not leave the program in an acceptable state (with the
// references created, and so on), so anyone calling this must fix that later.
//-----------------------------------------------------------------------------
void SolveSpace::ClearExisting(void) {
UndoClearStack(&redo);
UndoClearStack(&undo);
Group *g;
for(g = SK.group.First(); g; g = SK.group.NextAfter(g)) {
g->Clear();
}
SK.constraint.Clear();
SK.request.Clear();
SK.group.Clear();
SK.style.Clear();
SK.entity.Clear();
SK.param.Clear();
}
hGroup SolveSpace::CreateDefaultDrawingGroup(void) {
Group g;
ZERO(&g);
@ -24,16 +47,7 @@ hGroup SolveSpace::CreateDefaultDrawingGroup(void) {
}
void SolveSpace::NewFile(void) {
UndoClearStack(&redo);
UndoClearStack(&undo);
SK.constraint.Clear();
SK.request.Clear();
SK.group.Clear();
SK.style.Clear();
SK.entity.Clear();
SK.param.Clear();
ClearExisting();
// Our initial group, that contains the references.
Group g;
@ -393,15 +407,8 @@ bool SolveSpace::LoadFromFile(char *filename) {
return false;
}
UndoClearStack(&redo);
UndoClearStack(&undo);
ClearExisting();
SK.constraint.Clear();
SK.request.Clear();
SK.group.Clear();
SK.entity.Clear();
SK.param.Clear();
SK.style.Clear();
memset(&sv, 0, sizeof(sv));
sv.g.scale = 1; // default is 1, not 0; so legacy files need this

View File

@ -7,6 +7,27 @@ const hGroup Group::HGROUP_REFERENCES = { 1 };
#define gs (SS.GW.gs)
//-----------------------------------------------------------------------------
// The group structure includes pointers to other dynamically-allocated
// memory. This clears and frees them all.
//-----------------------------------------------------------------------------
void Group::Clear(void) {
polyLoops.Clear();
bezierLoops.Clear();
bezierOpens.Clear();
thisMesh.Clear();
runningMesh.Clear();
thisShell.Clear();
runningShell.Clear();
displayMesh.Clear();
displayEdges.Clear();
impMesh.Clear();
impShell.Clear();
impEntity.Clear();
// remap is the only one that doesn't get recreated when we regen
remap.Clear();
}
void Group::AddParam(IdList<Param,hParam> *param, hParam hp, double v) {
Param pa;
memset(&pa, 0, sizeof(pa));

View File

@ -189,6 +189,7 @@ public:
void Activate(void);
char *DescriptionString(void);
void Clear(void);
static void AddParam(ParamList *param, hParam hp, double v);
void Generate(EntityList *entity, ParamList *param);

View File

@ -679,6 +679,7 @@ public:
bool OkayToStartNewFile(void);
hGroup CreateDefaultDrawingGroup(void);
void UpdateWindowTitle(void);
void ClearExisting(void);
void NewFile(void);
bool SaveToFile(char *filename);
bool LoadFromFile(char *filename);

View File

@ -93,23 +93,10 @@ void SolveSpace::PopOntoCurrentFrom(UndoStack *uk) {
UndoState *ut = &(uk->d[uk->write]);
int i;
// Free everything in the main copy of the program before replacing it
for(i = 0; i < SK.group.n; i++) {
Group *g = &(SK.group.elem[i]);
g->polyLoops.Clear();
g->bezierLoops.Clear();
g->bezierOpens.Clear();
g->thisMesh.Clear();
g->runningMesh.Clear();
g->thisShell.Clear();
g->runningShell.Clear();
g->displayMesh.Clear();
g->displayEdges.Clear();
g->remap.Clear();
g->impMesh.Clear();
g->impShell.Clear();
g->impEntity.Clear();
Group *g;
for(g = SK.group.First(); g; g = SK.group.NextAfter(g)) {
g->Clear();
}
SK.group.Clear();
SK.request.Clear();

View File

@ -1,7 +1,6 @@
replace show/hide links with icons
add checked/unchecked checkbox and radio button
fix bug with rotation in plane where green line stays displayed
fix memory leak when switching between files
area of sketch in a given workplane (easy, useful)
lock point where dragged constraint
remove toolbar icons for sketch in 3d, add View -> Align to Workplane