Skip to content

Modernize memory management of Core Foundation pointers #3164

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ChrisThrasher
Copy link
Member

@ChrisThrasher ChrisThrasher commented Jul 15, 2024

Description

Follows the same pattern as #2540, #2759, #2801, and #3149.

Core Foundation pointers are used in a slightly more complicated way than in the previously cited PRs. In some cases CFRetain is called on a given pointer. I don't entirely understand how retains and releases work in the Apple Objective-C world so I chose not to touch pointers which are retained.

I believe this fixes a memory leak here:

CFArrayRef underlying = IOHIDDeviceCopyMatchingElements(keyboard, nullptr, kIOHIDOptionsTypeNone);
if ((underlying == nullptr) || (CFArrayGetCount(underlying) == 0))
{
err() << "Detected a keyboard without any keys." << std::endl;
return;
}

If underlying is non-null and CFArrayGetCount(underlying) returns zero then we exit the function without releasing underlying. This PR's new RAII-based approach guarantees that underlying gets freed.


Edit: I tried improving upon the pointers that did use CFRetain now that I better understand what retains and releases are. I'm still not entirely what I wrote is correct so this PR will have to sit for a bit longer before I'm ready to mark it as ready for review.

@ChrisThrasher ChrisThrasher added this to the 3.0 milestone Jul 15, 2024
@ChrisThrasher ChrisThrasher force-pushed the cf_pointers branch 2 times, most recently from 8018ff4 to 2abfc25 Compare July 15, 2024 20:42
@coveralls
Copy link
Collaborator

coveralls commented Jul 15, 2024

Pull Request Test Coverage Report for Build 12539613077

Details

  • 43 of 82 (52.44%) changed or added relevant lines in 8 files are covered.
  • 9 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.06%) to 57.087%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/SFML/Window/macOS/HIDJoystickManager.cpp 6 8 75.0%
src/SFML/Window/macOS/InputImpl.mm 0 4 0.0%
src/SFML/Window/macOS/cg_sf_conversion.mm 3 8 37.5%
src/SFML/Window/macOS/JoystickImpl.cpp 0 28 0.0%
Files with Coverage Reduction New Missed Lines %
src/SFML/Window/macOS/JoystickImpl.cpp 4 6.23%
src/SFML/Window/macOS/HIDInputManager.mm 5 57.89%
Totals Coverage Status
Change from base Build 12509336161: 0.06%
Covered Lines: 12044
Relevant Lines: 19954

💛 - Coveralls

@@ -40,7 +41,7 @@
namespace sf::priv
{

using IOHIDElements = std::vector<IOHIDElementRef>;
using IOHIDElements = std::vector<CFPtr<__IOHIDElement>>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know much about Obj-C, but should we really be using types that start with __?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was hesitant to use types with those names too. Does Apple not want users to directly use such types? I'm not sure. A much more verbose alternative would be std::remove_pointer_t<IOHIDElementRef>.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably go with that. Not a fan of using "reserved"/internal symbols

@ChrisThrasher ChrisThrasher modified the milestones: 3.0, 3.1 Aug 30, 2024
@ChrisThrasher
Copy link
Member Author

Moving this to the v3.1 milestone. I want to finish this eventually but for now it's not a priority to land in v3.0.

@ChrisThrasher ChrisThrasher force-pushed the cf_pointers branch 2 times, most recently from 568ba5f to e432237 Compare September 16, 2024 23:32
@ChrisThrasher ChrisThrasher force-pushed the cf_pointers branch 2 times, most recently from d29cdfa to 7f43eb8 Compare December 29, 2024 23:35
@WarlockD
Copy link

WarlockD commented Jun 6, 2025

Just curious but could this be wrapped in a simple std::shared_ptr type class? Hell even unique_ptr can be given a function (or in C++17 case a class) that could call release once its done with the pointer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: In Review
Development

Successfully merging this pull request may close these issues.

4 participants