fixed memory leak in Autoarr

This commit is contained in:
2022-02-14 17:16:58 +03:00
parent 86bf4790a2
commit e5ada61339
11 changed files with 145 additions and 113 deletions

View File

@@ -1,5 +1,4 @@
#include "SearchTree.h"
#include "../tests/tests.h"
STNode* STNode_create(){
STNode* node=malloc(sizeof(STNode));
@@ -9,8 +8,7 @@ STNode* STNode_create(){
return node;
}
uint8 nodn=0;
void STNode_free(STNode* node){dbg(0);nodn++;
void STNode_free(STNode* node){
if (!node) throw(ERR_NULLPTR);
if(node->branches){
for(uint8 n32 = 0;n32<8;n32++){
@@ -31,12 +29,26 @@ void STNode_free(STNode* node){dbg(0);nodn++;
}
}
free(node->branches);
}dbg(1);
//if value is not freed ptr
if(node->value.type>12 && node->value.type<21 && node->value.VoidPtr)
free(node->value.VoidPtr);dbg(2);printf("nodn %u\n",nodn);
printstnode(node);
free(node);dbg(3);nodn--;
}
if(node->value.VoidPtr)
switch (node->value.type) {
case Int8Ptr: case UInt8Ptr:
case Int16Ptr: case UInt16Ptr:
case Int32Ptr: case UInt32Ptr:
case Int64Ptr: case UInt64Ptr:
free(node->value.VoidPtr);
break;
case AutoarrPtr:
Autoarr_clear((Autoarr*)node->value.VoidPtr);
free(node->value.VoidPtr);
break;
case STNodePtr:
STNode_free((STNode*)node->value.VoidPtr);
break;
default: // value is not ptr
break;
}
free(node);
}
typedef struct {uint8 n32, n4, rem;} indexes3;
@@ -51,10 +63,9 @@ indexes3 splitindex(uint8 i){
void ST_push(STNode* node_first, const char* key, Unitype value){
if (!node_first) throw(ERR_NULLPTR);
char c=*(key++);
STNode* node_last=node_first;
while(c){
indexes3 i3=splitindex((uint8)c);
while(*key){
indexes3 i3=splitindex((uint8)*key);
if(!node_last->branches){
node_last->branches=(STNode****)malloc(8*sizeof(STNode*));
for(uint8 i=0;i<8;i++)
@@ -69,24 +80,22 @@ void ST_push(STNode* node_first, const char* key, Unitype value){
node_last->branches[i3.n32][i3.n4]=(STNode**)malloc(4*sizeof(STNode*));
for(uint8 i=0;i<4;i++)
node_last->branches[i3.n32][i3.n4][i]=(STNode*)NULL;
node_last->branches[i3.n32][i3.n4]=node_last->branches[i3.n32][i3.n4];
}
if(!node_last->branches[i3.n32][i3.n4][i3.rem])
node_last->branches[i3.n32][i3.n4][i3.rem]=STNode_create();
node_last=node_last->branches[i3.n32][i3.n4][i3.rem];
c=*(key++);
key++;
}
node_last->value=value;
}
const Unitype UnitypeNull={Null,.VoidPtr=NULL};
const Unitype UnitypeNull={.type=Null,.VoidPtr=NULL};
Unitype ST_pull(STNode* node_first, const char* key){
if (!node_first) throw(ERR_NULLPTR);
char c = *key;
STNode* node_last=node_first;
for (uint16 i=0;c!='\0';){
indexes3 i3=splitindex((uint8)c);
while (*key){
indexes3 i3=splitindex((uint8)*key);
if(!node_last->branches) return UnitypeNull;
STNode*** ptrn32=(STNode***)node_last->branches[i3.n32];
if(!ptrn32) return UnitypeNull;
@@ -94,7 +103,7 @@ Unitype ST_pull(STNode* node_first, const char* key){
if(!ptrn4) return UnitypeNull;
node_last=ptrn4[i3.rem];
if(!node_last) return UnitypeNull;
c=*(key+(++i));
key++;
}
return node_last->value;
}