Thanks to visit codestin.com
Credit goes to github.com

Skip to content

MNT: Change objective C code to Automatic Reference Counting (ARC) #23060

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

Merged
merged 2 commits into from
Jun 26, 2022

Conversation

greglucas
Copy link
Contributor

PR Summary

This updates the objective C macosx backend to use automatic reference counting instead of relying on manual tracking and releasing of the memory. It seems like this is recommended now, but I am definitely not an expert in this.

PR Checklist

Tests and Styling

  • [N/A] Has pytest style unit tests (and pytest passes).
  • [N/A] Is Flake 8 compliant (install flake8-docstrings and run flake8 --docstring-convention=all).

Documentation

  • [N/A] New features are documented, with examples if plot related.
  • [N/A] New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • [N/A] API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).
  • [N/A] Documentation is sphinx and numpydoc compliant (the docs should build without error).

@greglucas greglucas added this to the v3.6.0 milestone May 18, 2022
@jklymak
Copy link
Member

jklymak commented May 18, 2022

The backend seems to continue to function after this. However, I'm not sure how you would like it tested.

@greglucas
Copy link
Contributor Author

I'm not sure it is "testable"... Here is the traceback I was using to make it compatible if I enable ARC on main currently. This was in an effort to trace down the memory leaks.

Here is the ARC recommendation: https://microsoft.github.io/objc-guide/MethodsAndImplementations/AutomaticReferenceCounting.html#convention

expand for traceback
    src/_macosx.m:336:17: error: 'release' is unavailable: not available in automatic reference counting mode
        [self->view release];
                    ^
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/objc/NSObject.h:37:1: note: 'release' has been explicitly marked unavailable here
    - (oneway void)release OBJC_ARC_UNAVAILABLE;
    ^
    src/_macosx.m:336:17: error: ARC forbids explicit message send of 'release'
        [self->view release];
         ~~~~~~~~~~ ^
    src/_macosx.m:344:45: error: cast of Objective-C pointer type 'View *' to C pointer type 'void *' requires a bridged cast
                                   (void*)self, (void*)(self->view));
                                                ^~~~~~~~~~~~~~~~~~~
    src/_macosx.m:344:46: note: use __bridge to convert directly (no change in ownership)
                                   (void*)self, (void*)(self->view));
                                                 ^
                                                 __bridge
    src/_macosx.m:344:52: note: use CFBridgingRetain call to make an ARC object available as a +1 'void *'
                                   (void*)self, (void*)(self->view));
                                                       ^
                                                       CFBridgingRetain
    src/_macosx.m:548:17: error: 'release' is unavailable: not available in automatic reference counting mode
            [window release];
                    ^
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/objc/NSObject.h:37:1: note: 'release' has been explicitly marked unavailable here
    - (oneway void)release OBJC_ARC_UNAVAILABLE;
    ^
    src/_macosx.m:548:17: error: ARC forbids explicit message send of 'release'
            [window release];
             ~~~~~~ ^
    src/_macosx.m:601:46: error: cast of Objective-C pointer type 'Window *' to C pointer type 'void *' requires a bridged cast
                                   (void*) self, (void*)(self->window));
                                                 ^~~~~~~~~~~~~~~~~~~~~
    src/_macosx.m:601:47: note: use __bridge to convert directly (no change in ownership)
                                   (void*) self, (void*)(self->window));
                                                  ^
                                                  __bridge
    src/_macosx.m:601:53: note: use CFBridgingRetain call to make an ARC object available as a +1 'void *'
                                   (void*) self, (void*)(self->window));
                                                        ^
                                                        CFBridgingRetain
    src/_macosx.m:651:81: error: ARC forbids explicit message send of 'autorelease'
            NSImage* image = [[[NSImage alloc] initByReferencingFile: ns_icon_path] autorelease];
                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
    src/_macosx.m:651:81: error: 'autorelease' is unavailable: not available in automatic reference counting mode
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/objc/NSObject.h:38:1: note: 'autorelease' has been explicitly marked unavailable here
    - (instancetype)autorelease OBJC_ARC_UNAVAILABLE;
    ^
    src/_macosx.m:682:60: error: ARC forbids explicit message send of 'autorelease'
                               encoding: NSUTF8StringEncoding] autorelease];
                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
    src/_macosx.m:682:60: error: 'autorelease' is unavailable: not available in automatic reference counting mode
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/objc/NSObject.h:38:1: note: 'autorelease' has been explicitly marked unavailable here
    - (instancetype)autorelease OBJC_ARC_UNAVAILABLE;
    ^
    src/_macosx.m:769:67: error: must explicitly describe intended ownership of an object array parameter
    - (void)installCallbacks:(SEL[7])actions forButtons:(NSButton*[7])buttons;
                                                                      ^
    src/_macosx.m:795:67: error: must explicitly describe intended ownership of an object array parameter
    - (void)installCallbacks:(SEL[7])actions forButtons:(NSButton*[7])buttons
                                                                      ^
    src/_macosx.m:790:5: error: the result of a delegate init call must be immediately returned or assigned to 'self'
        [self init];
        ^~~~~~~~~~~
    src/_macosx.m:836:18: error: 'release' is unavailable: not available in automatic reference counting mode
            [handler release];
                     ^
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/objc/NSObject.h:37:1: note: 'release' has been explicitly marked unavailable here
    - (oneway void)release OBJC_ARC_UNAVAILABLE;
    ^
    src/_macosx.m:836:18: error: ARC forbids explicit message send of 'release'
            [handler release];
             ~~~~~~~ ^
    src/_macosx.m:928:21: error: 'release' is unavailable: not available in automatic reference counting mode
            [buttons[i] release];
                        ^
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/objc/NSObject.h:37:1: note: 'release' has been explicitly marked unavailable here
    - (oneway void)release OBJC_ARC_UNAVAILABLE;
    ^
    src/_macosx.m:928:21: error: ARC forbids explicit message send of 'release'
            [buttons[i] release];
             ~~~~~~~~~~ ^
    src/_macosx.m:929:16: error: 'release' is unavailable: not available in automatic reference counting mode
            [image release];
                   ^
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/objc/NSObject.h:37:1: note: 'release' has been explicitly marked unavailable here
    - (oneway void)release OBJC_ARC_UNAVAILABLE;
    ^
    src/_macosx.m:929:16: error: ARC forbids explicit message send of 'release'
            [image release];
             ~~~~~ ^
    fatal error: too many errors emitted, stopping now [-ferror-limit=]
    20 errors generated.
    error: command '/usr/bin/clang' failed with exit code 1

@QuLogic QuLogic added the CI: Run cibuildwheel Run wheel building tests on a PR label May 20, 2022
Copy link
Member

@jklymak jklymak left a comment

Choose a reason for hiding this comment

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

I don't know anything about this, other than that this PR works on my machine and it doesn't appear to do anything malicious. Is there anyone else who ha sufficient macOS experience to vouch for this? Also is ARC sufficiently back compatible?

@greglucas
Copy link
Contributor Author

Also is ARC sufficiently back compatible?

It came in MacOSX 10.7, so it has been around for quite a while now. Note that I did put a check at the top of the file as well so this will throw an error during compile if ARC is not enabled.

@jklymak
Copy link
Member

jklymak commented Jun 23, 2022

marking as merge w/ one review unless another Mac user wants to review I will merge in a week or so...

@@ -1064,36 +1060,6 @@ + (WindowServerConnectionManager *)sharedManager
return sharedWindowServerConnectionManager;
}

+ (id)allocWithZone:(NSZone *)zone
Copy link
Member

Choose a reason for hiding this comment

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

https://developer.apple.com/documentation/objectivec/nsobject/1571945-allocwithzone

Do not override allocWithZone: to include any initialization code. Instead, class-specific versions of init... methods.

OK, good thing we are deleting it?

Copy link
Member

Choose a reason for hiding this comment

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

it is also not clear we used this as the actual call site just above uses [[super allocWithZone:Null] init] ?

Copy link
Member

Choose a reason for hiding this comment

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

This all came in via 7bacdc5 with the commit message

This patch allows interrupts to be delivered once Python is fixed.

greglucas and others added 2 commits June 24, 2022 20:08
This updates the objective C macosx backend to use automatic reference
counting instead of relying on manual tracking and releasing of the memory.
@tacaswell
Copy link
Member

I took the liberty of rebasing and removing the line that was breaking the wheel build.

Copy link
Member

@tacaswell tacaswell left a comment

Choose a reason for hiding this comment

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

Please merge this @greglucas if you are happy with the commit I added.

@greglucas
Copy link
Contributor Author

Yes, that commit looks good, it must have come in since the last rebase. Thanks for fixing it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI: Run cibuildwheel Run wheel building tests on a PR GUI: MacOSX
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants