Difference between revisions of "Using ARC in tweaks"

From iPhone Development Wiki
Jump to: navigation, search
(How should I use ARC in tweaks?)
(How should I use ARC in tweaks?)
Line 10: Line 10:
 
It is not recommended that you use ARC in any "hooks", unless if you absolutely know what you are doing. Hook methods are slightly different from your typical Objective-C class declarations, and as such, the compiler has a more limited concept of ownership due to the nature of these hooks. In most cases it would be totally fine, but there *are* certain cases where you would get unexpected behavior, so in general it ends up being less of a headache if you avoid ARC altogether. Also, typically with hook methods, the compiler sees you passing objective-C objects to and from vanilla C methods which requires (__bridge) casts which ends up being more annoying than manually managing memory.
 
It is not recommended that you use ARC in any "hooks", unless if you absolutely know what you are doing. Hook methods are slightly different from your typical Objective-C class declarations, and as such, the compiler has a more limited concept of ownership due to the nature of these hooks. In most cases it would be totally fine, but there *are* certain cases where you would get unexpected behavior, so in general it ends up being less of a headache if you avoid ARC altogether. Also, typically with hook methods, the compiler sees you passing objective-C objects to and from vanilla C methods which requires (__bridge) casts which ends up being more annoying than manually managing memory.
  
However, if you are declaring new classes, or subclassing existing ones (not hooking), then ARC will behave exactly as one would expect it to behave and it would be totally fine to use.
+
However, if you are declaring new classes, or subclassing existing ones (not hooking), then ARC will behave exactly as one would expect it to behave and it would be totally fine to use. However, even at that point some people consider it to be a bad idea to use because generally you *never* use ARC in your hook methods, so there ends up being this disconnect between your different implementation files, and the programmer ends up forgetting which files use ARC and which files don't, which can lead to memory leaks.
  
It's a good idea to remember to separate these bits into different implementation files, so you can compile your class declarations with -fobjc-arc, and your hook declarations without the flag, and then just linking them together into your binary.
+
So generally, the answer is no. Don't use ARC.

Revision as of 12:01, 25 January 2015

What is ARC?

In Objective-C and Swift programming, Automatic Reference Counting (ARC) is a memory management enhancement where the burden of keeping track of an object's reference count is lifted from the programmer to the compiler. In traditional Objective-C, the programmer would send retain and release messages to objects in order to mark objects for deallocation or to prevent deallocation. Under ARC, the compiler does this automatically by examining the source code and then adding the retain and release messages in the compiled code.

How should I use ARC in tweaks?

It is not recommended that you use ARC in any "hooks", unless if you absolutely know what you are doing. Hook methods are slightly different from your typical Objective-C class declarations, and as such, the compiler has a more limited concept of ownership due to the nature of these hooks. In most cases it would be totally fine, but there *are* certain cases where you would get unexpected behavior, so in general it ends up being less of a headache if you avoid ARC altogether. Also, typically with hook methods, the compiler sees you passing objective-C objects to and from vanilla C methods which requires (__bridge) casts which ends up being more annoying than manually managing memory.

However, if you are declaring new classes, or subclassing existing ones (not hooking), then ARC will behave exactly as one would expect it to behave and it would be totally fine to use. However, even at that point some people consider it to be a bad idea to use because generally you *never* use ARC in your hook methods, so there ends up being this disconnect between your different implementation files, and the programmer ends up forgetting which files use ARC and which files don't, which can lead to memory leaks.

So generally, the answer is no. Don't use ARC.