Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dual mode ARC/GC and Core Foundation bridging

I am writing code intended to work both under ARC and under Garbage Collection.

Here's a bit of code that uses Core Foundation as it might be written specifically for ARC:

CFTypeRef ref=CFCopySomething();
// At this point ref has retain count 1.
id obj=(__bridge_transfer id)ref;
// Ref still has retain count 1 but is now managed by ARC.
[obj doSomething];
// ARC will release ref when done.

It seems this is equivalent to:

CFTypeRef ref=CFCopySomething();
// At this point ref has retain count 1.
id obj=(__bridge id)ref;
// Now ref has retain count 2 due to assigning to strong variable under ARC.
CFRelease(ref)
// Now ref has retain count 1.
[obj doSomething];
// ARC will release ref when done.

The benefit of the latter being that the CFRelease call allows the GC to collect the object. But I'm not sure about calling the CFRelease after transferring to ARC with the bridge-casted assignment.

It certainly seems to work. Is this code OK?

like image 610
Nick Moore Avatar asked Feb 27 '12 16:02

Nick Moore


People also ask

What is the 5GC architecture?

The 5GC architecture enables fast new service creation and extendibility, using software-based APIs to efficiently configure and connect core network functionality. This can potentially change the way service providers interact with the enterprise industry and how they explore new business model opportunities with industries and partners.

What are the longitudinal girders of the bridge deck?

The longitudinal girders of the bridge deck are 5 in number with dimensions of 1000 mm x 400 mm, spaced at 2 m centre to centre. They are supported by transverse girders of the same dimension, which transfer the deck load to the columns. The columns (spandrels) ultimately transfer the deck load to the arch ribs.

What are the structural features of an arch bridge?

The structural scheme adopted in any arch bridge can be influenced by a lot of factors such as the type of deck, environmental conditions, cost, and feasibility. However in terms of structural form, arches can be broadly classified as hinged or fixed.

How will the dual-mode solution help service providers migrate to 5G?

By implementing the dual-mode solution service providers will have more control to migrate to 5G in their own time and in alignment with their business needs.


2 Answers

Your second code snippet is correct, and indeed is the best way to handle both ARC and GC. You could also use CFMakeCollectable when creating the object, and then have the CFRelease done as follows:

if ([NSGarbageCollector defaultCollector] == NULL) CFRelease(myCFString)

But I like better what you have with just one call that works for both environments.

like image 147
charles Avatar answered Sep 18 '22 00:09

charles


Nick,

As the CFObjects are not handled by ARC, you may actually want to keep the manually managed code here. ARC is really focused on Cocoa and not Core Foundation. That said, you said the code works but does it leak? Remember ARC code with the wrong compiler flags fails by leaking. In this Apple documentation, they claim that ARC does not manage CF objects: https://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html. Hence, I think your __bridge code leaks and await your confirmation or rejection from Instruments' leaks tool.

Andrew

like image 37
adonoho Avatar answered Sep 21 '22 00:09

adonoho