Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent leaking memory on cursors #194

Merged
merged 1 commit into from
Mar 28, 2022
Merged

Prevent leaking memory on cursors #194

merged 1 commit into from
Mar 28, 2022

Conversation

LeonSkoog
Copy link

Hello,
It's my first PR to this project, I don't know what is the correct procedure yet :)

During the Init process, bunch of cursors being allocated:

// load mouse cursors
loadMouseCursor(ImGuiMouseCursor_Arrow, sf::Cursor::Arrow);
loadMouseCursor(ImGuiMouseCursor_TextInput, sf::Cursor::Text);
loadMouseCursor(ImGuiMouseCursor_ResizeAll, sf::Cursor::SizeAll);
loadMouseCursor(ImGuiMouseCursor_ResizeNS, sf::Cursor::SizeVertical);
loadMouseCursor(ImGuiMouseCursor_ResizeEW, sf::Cursor::SizeHorizontal);
loadMouseCursor(ImGuiMouseCursor_ResizeNESW, sf::Cursor::SizeBottomLeftTopRight);
loadMouseCursor(ImGuiMouseCursor_ResizeNWSE, sf::Cursor::SizeTopLeftBottomRight);
loadMouseCursor(ImGuiMouseCursor_Hand, sf::Cursor::Hand);

loadMouseCursor function allocates memory for cursor and marks whether loading the cursor succeed or not:

void loadMouseCursor(ImGuiMouseCursor imguiCursorType, sf::Cursor::Type sfmlCursorType) {
     s_currWindowCtx->mouseCursors[imguiCursorType] = new sf::Cursor();
     s_currWindowCtx->mouseCursorLoaded[imguiCursorType] =
     s_currWindowCtx->mouseCursors[imguiCursorType]->loadFromSystem(sfmlCursorType);
}

Destructor deletes only cursors marked as 'loaded':

     ~WindowContext() {
         delete fontTexture;
         for (int i = 0; i < ImGuiMouseCursor_COUNT; ++i) {
             if (mouseCursorLoaded[i]) {
                 delete mouseCursors[i];
             }
         }
         ImGui::DestroyContext(imContext);
    }

However, it's very much possible that the cursor couldn't be loaded and the space for it won't be de-allocated. Which leaks the memory.

My PR is a proposal of handling the memory leak.

- delete all allocated cursors
@lefticus
Copy link

lefticus commented Mar 6, 2022

I just hit this exact issue while trying to demo some sfml/imgui work with ASAN enabled.

@eliasdaler eliasdaler merged commit 03c3e30 into SFML:master Mar 28, 2022
@eliasdaler
Copy link
Contributor

That's a good catch!
Sorry it took me so long to take to this. Thanks for the PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants