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

Use NS::TransferPtr instead of NS::RetainPtr #2254

Merged
merged 2 commits into from
Apr 11, 2024

Conversation

louwers
Copy link
Collaborator

@louwers louwers commented Apr 11, 2024

I think this fixes a memory leak.

->alloc->init() sets the retain count to 1, then creating NS::RetainPtr increases it to two. The destructor of the shared pointer reduces the retain count to 1, resulting in a memory leak. So instead a NS::TransferPtr should be used. This is also what is done in other places of the codebase when calling ->alloc()->init()

/**
 * Create a SharedPtr by retaining an existing raw pointer.
 * Increases the reference count of the passed-in object.
 * If the passed-in object was in an AutoreleasePool, it will be removed from it.
 */
template <class _Class>
_NS_INLINE NS::SharedPtr<_Class> RetainPtr(_Class* pObject)
{
    NS::SharedPtr<_Class> ret;
    ret.m_pObject = pObject->retain();
    return ret;
}

@louwers louwers added bug Something isn't working metal labels Apr 11, 2024
@louwers louwers self-assigned this Apr 11, 2024
Copy link

Bloaty Results (iOS) 🐋

Compared to main

    FILE SIZE        VM SIZE    
 --------------  -------------- 
  [ = ]       0  [ = ]       0    TOTAL

Full report: https://maplibre-native.s3.eu-central-1.amazonaws.com/bloaty-results-ios/pr-2254-compared-to-main.txt

Copy link

Bloaty Results 🐋

Compared to main

    FILE SIZE        VM SIZE    
 --------------  -------------- 
  [ = ]       0  [ = ]       0    TOTAL

Full report: https://maplibre-native.s3.eu-central-1.amazonaws.com/bloaty-results/pr-2254-compared-to-main.txt

Compared to d387090 (legacy)

    FILE SIZE        VM SIZE    
 --------------  -------------- 
   +20% +23.0Mi  +405% +24.2Mi    TOTAL

Full report: https://maplibre-native.s3.eu-central-1.amazonaws.com/bloaty-results/pr-2254-compared-to-legacy.txt

Copy link

Benchmark Results ⚡

Benchmark                                                     Time             CPU      Time Old      Time New       CPU Old       CPU New
------------------------------------------------------------------------------------------------------------------------------------------
OVERALL_GEOMEAN                                            +0.0015         +0.0016             0             0             0             0

Full report: https://maplibre-native.s3.eu-central-1.amazonaws.com/benchmark-results/pr-2254-compared-to-main.txt

@louwers louwers merged commit 9f05eaa into maplibre:main Apr 11, 2024
32 of 33 checks passed
@louwers louwers deleted the fix-memoryleak branch April 11, 2024 12:36
@TimSylvester TimSylvester mentioned this pull request Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working metal
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants