Retaining ARC objects in c++ classes

后端 未结 2 643
傲寒
傲寒 2021-02-19 12:07

I have some code that must remain as c++, but I need to store objective-c objects in these c++ classes. The objects will not be referenced anywhere else while they are stored h

2条回答
  •  面向向阳花
    2021-02-19 12:33

    You can call CFRetain and CFRelease under ARC. You are responsible for balancing each CFRetain with a CFRelease, because ARC does not pay any attention to these functions.

    There is no CFAutorelease function. You could perform an autorelease using objc_msgSend(myObject, sel_registerName("autorelease")). The hoops you are jumping through here should be a red flag that you're doing something that's probably wrong.

    Generally it's better under ARC to find a way not to store object references in otherwise-untyped blobs of memory. Note that member variables of C++ objects can be qualified __strong or __weak if you compile them as Objective-C++.

    UPDATE

    The public SDKs of Mac OS X 10.9 and iOS 7.0 include a CFAutorelease function, which you can use even under ARC.

提交回复
热议问题