Skip to content

fix(memory): Fixed memory leaks at shutdown#6667

Open
bassdr wants to merge 3 commits into
HarbourMasters:developfrom
bassdr:fix/shutdown-crash
Open

fix(memory): Fixed memory leaks at shutdown#6667
bassdr wants to merge 3 commits into
HarbourMasters:developfrom
bassdr:fix/shutdown-crash

Conversation

@bassdr

@bassdr bassdr commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Was not cleaning up the context properly, leaving things initialized while other were getting deleted. I did not run valgrind or such, so there are probably other memory leak.

Build Artifacts

@Pepper0ni

Copy link
Copy Markdown
Contributor

There's already a fix for the crash at least, but that is currently on a different branch due to LUS development pipelines being in a bad place. Kenix3/libultraship#1103

Said Pipelines mean there's a good chance the LUS PR will not be accepted for a while.

@bassdr bassdr force-pushed the fix/shutdown-crash branch from 24b3e3c to d3dd44b Compare June 4, 2026 04:38
@serprex serprex added the bug Something isn't working label Jun 6, 2026
@bassdr bassdr force-pushed the fix/shutdown-crash branch 3 times, most recently from f1f523b to da946aa Compare June 8, 2026 23:42
@bassdr

bassdr commented Jun 9, 2026

Copy link
Copy Markdown
Contributor Author

I update this PR

So this PR does NOT depend on any version of LUS anymore, it can be merged as is.

@Malkierian

Copy link
Copy Markdown
Contributor

Why don't you think pinning port-maintenance is the answer?

@bassdr

bassdr commented Jun 10, 2026

Copy link
Copy Markdown
Contributor Author

While I am not a fan of using std::shared_ptr to manage lifetime of objects, as it's much clearer IMO having clear ownership and use eg. std::unique_ptr or no pointer at all in the owner and let consumers use references to access it.

But, LUS is not built that way at all, it has std::shared_ptr everywhere, and lifetime of objects are managed by keeping a std::shared_ptr on the object while using it. Destruction of the objects happens when nobody uses them, no matter when this happens. While not my favorite pattern, it's defendable the way it's used in LUS. I feel like diverging from that with the GetRawInstance is moving away from this pattern, creating confusion in the design. My PR just embrases the design and release the shared_ptr only when done using it, delaying destruction just enough so we don't have issues.

Also, I am more interrested in #6668 landing, I think it's a HUGE audio quality improvement. I am done, just have to prepare a video or something so that people understand (aka. hear) the difference, it's actually amazing. A big chunk of the work was done in LUS itself, and it started from main. It's already tedious to have to pin my branch on LUS, I know have to also bring the changes from port-maintenance, and it's very annoying.

If you tell me the plan is to start a branch in LUS and use that one in shipwright, then I'll target that branch with my PRs in LUS (probably will end up having two PRs, for other games too). That can be a fine answer. I just don't know the plan right now, and I personally think we should stick on main, especially that this PR fixes the same issue that was adressed in #6629 still pinning main.

@bassdr bassdr force-pushed the fix/shutdown-crash branch 2 times, most recently from ee5e9b4 to 9f2b1b3 Compare June 11, 2026 20:47
@bassdr

bassdr commented Jun 11, 2026

Copy link
Copy Markdown
Contributor Author

I ended up removing the revert from here... so this is just memory management cleanup now.

@bassdr bassdr changed the title Fixed crash and memory leaks at shutdown Fixed memory leaks at shutdown Jun 11, 2026
@Malkierian

Copy link
Copy Markdown
Contributor

Sorry, missed the last comment until just now. The changes louis made for Context on port-maintenance is how we're planning on going with most of the rest of the shared_ptrs throughout the codebase, it was just a first step. Though, whether to use unique_ptr or raw pointers is something that could still be discussed, I think.

@bassdr bassdr changed the title Fixed memory leaks at shutdown fix(memory): Fixed memory leaks at shutdown Jun 11, 2026
@serprex serprex requested a review from Malkierian June 12, 2026 05:18
@bassdr bassdr force-pushed the fix/shutdown-crash branch from 9f2b1b3 to 247df5b Compare June 13, 2026 17:54
@bassdr

bassdr commented Jun 13, 2026

Copy link
Copy Markdown
Contributor Author

OK, thanks, I still think this PR worth merging IMO, even if it's not a matter of crashing at runtime anymore, releasing the shared_ptrs we do not use anymore is a sane thing to do, while we migrate to the new ownership pattern.

@bassdr bassdr force-pushed the fix/shutdown-crash branch from 247df5b to 1c3eedc Compare June 13, 2026 18:03
@mergify

mergify Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

bassdr and others added 2 commits June 23, 2026 10:28
Restores the guard from the original shutdown-crash fix that was dropped when
the branch was recreated: on the first OTRControllerCallback the window is
created in the if-branch, so the old else-if could dereference a null
controllerConfigWindow.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants