Skip to content

Commit

Permalink
19108: Fixes resource leak with strings that could also lead to erron…
Browse files Browse the repository at this point in the history
…eous mixing of strings when handling strings with few references (#64)
  • Loading branch information
howsohazard authored Jan 26, 2024
1 parent bf5fc88 commit 63be16b
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 61 deletions.
6 changes: 3 additions & 3 deletions src/Amalgam/AmalgamMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ PLATFORM_MAIN_CONSOLE
if(num_strings_used > 0)
{
std::cerr << "ERROR: Num strings still in use: " << num_strings_used << std::endl;
std::vector<std::string> in_use = string_intern_pool.GetNonStaticStringsInUse();
for(auto &s : in_use)
std::cerr << '"' << s << '"' << std::endl;
std::vector<std::pair<std::string, int64_t>> in_use = string_intern_pool.GetNonStaticStringsInUse();
for(auto &[s, count] : in_use)
std::cerr << '"' << s << "\":" << count << std::endl;
}

std::cout << "Memory reclaimation complete." << std::endl;
Expand Down
4 changes: 1 addition & 3 deletions src/Amalgam/interpreter/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ EvaluableNodeReference Interpreter::ConvertArgsToCallStack(EvaluableNodeReferenc
}
else if(!args.unique)
{
args.SetReference(enm.AllocNode(args));
args.SetReference(enm.AllocNode(args, EvaluableNodeManager::ENMM_REMOVE_ALL));
}

EvaluableNode *call_stack = enm.AllocNode(ENT_LIST);
Expand Down Expand Up @@ -675,7 +675,6 @@ double Interpreter::InterpretNodeIntoNumberValue(EvaluableNode *n)

double value = result_value.GetValueAsNumber();
evaluableNodeManager->FreeNodeTreeIfPossible(result);
result.FreeImmediateResources();

return value;
}
Expand Down Expand Up @@ -709,7 +708,6 @@ bool Interpreter::InterpretNodeIntoBoolValue(EvaluableNode *n, bool value_if_nul

bool value = result_value.GetValueAsBoolean();
evaluableNodeManager->FreeNodeTreeIfPossible(result);
result.FreeImmediateResources();

return value;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Amalgam/interpreter/Interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class Interpreter
if(EvaluableNode::IsAssociativeArray(new_context))
{
if(!new_context.unique)
new_context.SetReference(evaluableNodeManager->AllocNode(new_context));
new_context.SetReference(evaluableNodeManager->AllocNode(new_context, EvaluableNodeManager::ENMM_REMOVE_ALL));
}
else //not assoc, make a new one
{
Expand Down
16 changes: 9 additions & 7 deletions src/Amalgam/interpreter/InterpreterOpcodesBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_SEQUENCE(EvaluableNode *en
evaluableNodeManager->FreeNodeTreeIfPossible(result);
//request immediate values when not last, since any allocs for returns would be wasted
//concludes won't be immediate
result = InterpretNode(ocn[i], immediate_result || i < ocn_size - 1);
result = InterpretNode(ocn[i], immediate_result || i + 1 < ocn_size);
}
return result;
}
Expand Down Expand Up @@ -613,7 +613,7 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_WHILE(EvaluableNode *en, b
//concludes won't be immediate
//but because previous_result may be used, that can't be immediate, so the last param
//cannot be evaulated as immediate
new_result = InterpretNode(ocn[i], i < ocn_size - 1);
new_result = InterpretNode(ocn[i], i + 1 < ocn_size);

if(!new_result.IsImmediateValue() && new_result != nullptr && new_result->GetType() == ENT_CONCLUDE)
{
Expand All @@ -626,7 +626,7 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_WHILE(EvaluableNode *en, b
}

//don't free the last new_result
if(i < ocn_size - 1)
if(i + 1 < ocn_size)
evaluableNodeManager->FreeNodeTreeIfPossible(new_result);
}

Expand Down Expand Up @@ -666,7 +666,7 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_LET(EvaluableNode *en, boo
evaluableNodeManager->FreeNodeTreeIfPossible(result);
//request immediate values when not last, since any allocs for returns would be wasted
//concludes won't be immediate
result = InterpretNode(ocn[i], immediate_result || i < ocn_size - 1);
result = InterpretNode(ocn[i], immediate_result || i + 1 < ocn_size);
}

//all finished with new context, but can't free it in case returning something
Expand Down Expand Up @@ -770,7 +770,7 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_DECLARE(EvaluableNode *en,
evaluableNodeManager->FreeNodeTreeIfPossible(result);
//request immediate values when not last, since any allocs for returns would be wasted
//concludes won't be immediate
result = InterpretNode(ocn[i], immediate_result || i < ocn_size - 1);
result = InterpretNode(ocn[i], immediate_result || i + 1 < ocn_size);
}

return result;
Expand Down Expand Up @@ -1036,8 +1036,9 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_RETRIEVE(EvaluableNode *en
if(to_lookup == nullptr || IsEvaluableNodeTypeImmediate(to_lookup->GetType()))
{
StringInternPool::StringID symbol_name_sid = EvaluableNode::ToStringIDIfExists(to_lookup);
EvaluableNode* symbol_value = GetCallStackSymbol(symbol_name_sid);
evaluableNodeManager->FreeNodeTreeIfPossible(to_lookup);
return EvaluableNodeReference(GetCallStackSymbol(symbol_name_sid), false);
return EvaluableNodeReference(symbol_value, false);
}
else if(to_lookup->IsAssociativeArray())
{
Expand Down Expand Up @@ -1070,11 +1071,12 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_RETRIEVE(EvaluableNode *en
continue;
}

EvaluableNode *symbol_value = GetCallStackSymbol(symbol_name_sid);
//if there are values passed in, free them to be clobbered
EvaluableNodeReference cnr(cn, to_lookup.unique);
evaluableNodeManager->FreeNodeTreeIfPossible(cnr);

cn = GetCallStackSymbol(symbol_name_sid);
cn = symbol_value;
}

return EvaluableNodeReference(to_lookup, false);
Expand Down
3 changes: 2 additions & 1 deletion src/Amalgam/interpreter/InterpreterOpcodesEntityAccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,13 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_RETRIEVE_FROM_ENTITY_and_D
if(to_lookup == nullptr || IsEvaluableNodeTypeImmediate(to_lookup->GetType()))
{
StringInternPool::StringID label_sid = EvaluableNode::ToStringIDIfExists(to_lookup);
evaluableNodeManager->FreeNodeTreeIfPossible(to_lookup);

ExecutionCycleCount num_steps_executed = 0;
EvaluableNodeReference value = target_entity->GetValueAtLabel(label_sid, evaluableNodeManager, direct, target_entity == curEntity);
curExecutionStep += num_steps_executed;

evaluableNodeManager->FreeNodeTreeIfPossible(to_lookup);

return value;
}
else if(to_lookup->IsAssociativeArray())
Expand Down
82 changes: 41 additions & 41 deletions src/Amalgam/out.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ abcdef
4
"d"
)
(list 2 "d" 1 0 3)
(list 2 1 "d" 0 3)
(list
1
2
Expand Down Expand Up @@ -1240,7 +1240,7 @@ current_index: 2
8
)
accum_string "abcdef"
argv (list "C:\\Users\\Chris Hazard\\Desktop\\Howso_repos\\amalgam\\src\\Amalgam\\./amlg_code/full_test.amlg")
argv (list "C:\\Users\\ChristopherHazard\\Desktop\\Howso_repos\\amalgam\\src\\Amalgam\\./amlg_code/full_test.amlg")
bar (declare
(assoc x 6)
(+ x 2)
Expand All @@ -1253,10 +1253,10 @@ current_index: 2
A (assoc B 2)
B 2
)
interpreter "C:\\Users\\Chris Hazard\\Desktop\\Howso_repos\\amalgam\\x64\\MT_Release_EXE\\Amalgam.exe"
interpreter "C:\\Users\\ChristopherHazard\\Desktop\\Howso_repos\\amalgam\\x64\\MT_Release_EXE\\Amalgam.exe"
raaa 2
rwww 1
start_time 1706155111.185343
start_time 1706237472.540396
www 1
x 12
zz 10
Expand All @@ -1283,7 +1283,7 @@ current_index: 2
8
)
accum_string "abcdef"
argv (list "C:\\Users\\Chris Hazard\\Desktop\\Howso_repos\\amalgam\\src\\Amalgam\\./amlg_code/full_test.amlg")
argv (list "C:\\Users\\ChristopherHazard\\Desktop\\Howso_repos\\amalgam\\src\\Amalgam\\./amlg_code/full_test.amlg")
bar (declare
(assoc x 6)
(+ x 2)
Expand All @@ -1296,10 +1296,10 @@ current_index: 2
A (assoc B 2)
B 2
)
interpreter "C:\\Users\\Chris Hazard\\Desktop\\Howso_repos\\amalgam\\x64\\MT_Release_EXE\\Amalgam.exe"
interpreter "C:\\Users\\ChristopherHazard\\Desktop\\Howso_repos\\amalgam\\x64\\MT_Release_EXE\\Amalgam.exe"
raaa 2
rwww 1
start_time 1706155111.185343
start_time 1706237472.540396
www 1
x 12
zz 10
Expand All @@ -1325,7 +1325,7 @@ current_index: 2
8
)
accum_string "abcdef"
argv (list "C:\\Users\\Chris Hazard\\Desktop\\Howso_repos\\amalgam\\src\\Amalgam\\./amlg_code/full_test.amlg")
argv (list "C:\\Users\\ChristopherHazard\\Desktop\\Howso_repos\\amalgam\\src\\Amalgam\\./amlg_code/full_test.amlg")
bar (declare
(assoc x 6)
(+ x 2)
Expand All @@ -1338,10 +1338,10 @@ current_index: 2
A (assoc B 2)
B 2
)
interpreter "C:\\Users\\Chris Hazard\\Desktop\\Howso_repos\\amalgam\\x64\\MT_Release_EXE\\Amalgam.exe"
interpreter "C:\\Users\\ChristopherHazard\\Desktop\\Howso_repos\\amalgam\\x64\\MT_Release_EXE\\Amalgam.exe"
raaa 2
rwww 1
start_time 1706155111.185343
start_time 1706237472.540396
www 1
x 12
zz 10
Expand Down Expand Up @@ -1489,7 +1489,7 @@ infinity test c or d: (list "c" @(get (target 0) 0) "d" @(get (target 0) 0))

(assoc a 29 b 44 c 27)

(list "5" "2" "1")
(list "7" "2" "1")

--get_rand_seed--
��ȼ�\�KOaV�T z�
Expand Down Expand Up @@ -1609,7 +1609,7 @@ e:
- .inf

25: (assoc a 1)
current date-time in epoch: 2024-01-24-22.58.31.5508810
current date-time in epoch: 2024-01-25-21.51.12.5883020
2020-06-07 00:22:59
1391230800
1391230800
Expand Down Expand Up @@ -2685,10 +2685,10 @@ MergeEntityChild1
(associate "x" 3 "y" 4)
MergeEntityChild2
(associate "p" 3 "q" 4)
_2169689611
(associate "e" 3 "f" 4)
_2280722175
(associate "E" 3 "F" 4)
_2169689611
(associate "e" 3 "f" 4)
--union_entities--
(associate "b" 4 "a" 3 "c" 3)
MergeEntityChild1
Expand All @@ -2706,17 +2706,6 @@ MergeEntityChild2
"w"
7
)
_2169689611
(associate
"e"
3
"f"
4
"g"
5
"h"
6
)
_2280722175
(associate
"E"
Expand All @@ -2728,11 +2717,7 @@ _2280722175
"H"
6
)
(parallel
##p
(list "_3990396532" "_3330773578" "_3990396532" "_3330773578")
)
_3330773578
_2169689611
(associate
"e"
3
Expand All @@ -2743,6 +2728,10 @@ _3330773578
"h"
6
)
(parallel
##p
(list "_3990396532" "_3330773578" "_3990396532" "_3330773578")
)
_3990396532
(associate
"E"
Expand All @@ -2754,6 +2743,17 @@ _3990396532
"H"
6
)
_3330773578
(associate
"e"
3
"f"
4
"g"
5
"h"
6
)
--difference_entities--
(declare
(assoc _ (null))
Expand Down Expand Up @@ -3375,17 +3375,17 @@ MergeEntityChild1
(associate "x" 3 "y" 4 "z" 5)
MergeEntityChild2
(associate "p" 3 "q" 4 "u" 5)
_2169689611
(associate "e" 3 "f" 4)
_2280722175
(associate "E" 3 "F" 4)
_2169689611
(associate
"E"
"e"
3
"F"
"f"
4
"G"
"g"
5
"H"
"h"
6
)
--get_entity_comments--
Expand Down Expand Up @@ -3442,7 +3442,7 @@ deep sets

--set_entity_root_permission--
RootTest
1706155111.773997
1706237472.781054
(true)

RootTest
Expand Down Expand Up @@ -3666,7 +3666,7 @@ hello
)
)
)
(set_entity_rand_seed new_entity "U�EO��0є-I���")
(set_entity_rand_seed new_entity "�^\n�brj�0є-I���")
(set_entity_rand_seed
(first
(create_entities
Expand All @@ -3676,7 +3676,7 @@ hello
)
)
)
"C(Űv���6�RbH��"
"5ȳ\0�Nb�h��e��"
)
(set_entity_rand_seed
(first
Expand Down Expand Up @@ -3709,7 +3709,7 @@ hello
)
)
)
(set_entity_rand_seed new_entity "U�EO��0є-I���")
(set_entity_rand_seed new_entity "�^\n�brj�0є-I���")
(set_entity_rand_seed
(first
(create_entities
Expand Down Expand Up @@ -4658,4 +4658,4 @@ Expecting 1000: 1000
concurrent entity writes successful: (true)

--total execution time--
1.1163010597229004
1.728956937789917
Loading

0 comments on commit 63be16b

Please sign in to comment.