Skip to content

Commit

Permalink
Close leak in throwing postinits
Browse files Browse the repository at this point in the history
OK, well this is embarrassing.  Despite asserting in the PR message of
inits in terms of what it did, apparently I never actually ran with
memleaks on to make sure that no memory was leaked when a postinit
throws, and...  Well, it was.  (I had inspected the generated code and
seen that we were emitting some code to handle its thrown errors, but
didn't look closely enough to see that that handling didn't actually
do any clean-up).  Sloppy.

Here, I'm fixing my issue by tucking the call to postinit() into the
same try...catch block as the throwing initializer code is put into,
which gives it the same outcome, which is to free the memory.  This
should resolve the memory leaks failures we saw on Dec 20, 2024.

---
Signed-off-by: Brad Chamberlain <[email protected]>
  • Loading branch information
bradcray committed Dec 21, 2024
1 parent 2cfd1a2 commit 6a74ca7
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions compiler/resolution/initializerResolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ static FnSymbol* buildNewWrapper(FnSymbol* initFn, Expr* allocator = nullptr) {
(type->hasPostInitializer() && type->postinit->throwsError())) {
fn->throwsErrorInit();
BlockStmt* tryBody = new BlockStmt(innerInit);
if (type->hasPostInitializer() == true) {
tryBody->insertAtTail(new CallExpr("postinit", gMethodToken, initTemp));
}

const char* errorName = astr("e");
BlockStmt* catchBody = new BlockStmt(callChplHereFree(initTemp));
Expand Down Expand Up @@ -227,11 +230,11 @@ static FnSymbol* buildNewWrapper(FnSymbol* initFn, Expr* allocator = nullptr) {

} else {
body->insertAtTail(innerInit);
if (type->hasPostInitializer() == true) {
body->insertAtTail(new CallExpr("postinit", gMethodToken, initTemp));
}
}

if (type->hasPostInitializer() == true) {
body->insertAtTail(new CallExpr("postinit", gMethodToken, initTemp));
}

VarSymbol* result = newTemp();
Expr* resultExpr = NULL;
Expand Down

0 comments on commit 6a74ca7

Please sign in to comment.