2

I studied a tutorial about Objective-C memory management and i think i understand how it works. However, i wonder why autorelease-pools were created to work the way they do. As far as i understand the autorelease message is mostly used when returning objects from functions, since the callee can't be sure that the caller will actually store the result in a variable (which would be necessary to release the returned object later on). I have a theory that this concept was made back in the day when Objective-C was merely a preprocessor. A compiler instead would be aware of an unassigned return value and could silently auto-insert a release for the returned object (this behaviour would imply that every assigned return value would have to be released manually)

So my questions:

  • Am i right with my theory or are there other reasons for the autorelease-pool concept
  • Are there other concepts to deal with the return-value problem for a retain/release based manual memory-management (not necessarily Objective-C-specific)
4
  • 1
    A sufficiently sophisticated static analyzer (Clang's) for Objective-C code only came about quite recently. This is what's required for the compiler to "be aware of an unassigned return value and [...] silently auto-insert a release for the returned object". The period of time between the Clang's static analyzer being released and ARC being added was short. Related: stackoverflow.com/q/8235946/1052673 Apr 10, 2013 at 12:00
  • @NateChandler the analyser is used to convert manually-counted code to automatically-referenced code. The inserted retained/releases are done by the compiler, with the transfer-across-function-return case being handled by runtime functions. If you have an Apple developer account, it's explained in this WWDC session: adcdownload.apple.com//wwdc_2011/…
    – user23743
    Apr 10, 2013 at 12:28
  • @GrahamLee Right, I understand that. My point is that the same technology used in the static analyzer is used in the compiler and that that technology only recently became available. Apr 10, 2013 at 12:52
  • 1
    @GrahamLee The analyzer's use for conversion was a convenience vs. the original purpose of the analyzer (busting invalid code patterns that weren't really a syntactic issue). The difference between ARC and the analyzer is primarily that ARC only deals with hard rules while eliminating ambiguity whereas the analyzer gets to be a bit fuzzy because it doesn't actually have to generate code. One of the few times where spewing English descriptions of something is easier than generating machine executable code! :)
    – bbum
    Apr 10, 2013 at 15:55

1 Answer 1

7

Your explanation is partially correct. Yes, autorelease pools are mainly used to return a non-owned object from a function or method: you can't release the object inside your method or the caller couldn't use it. You could require callers to accept ownership of an object returned from a function or method, as Core Foundation does with its Create rule, but Foundation introduced the autorelease pool and avoids this requirement.

However this is not related to Objective-C being a preprocessor or part of the compiler. It's dependent purely on the memory management policy in use by the framework and language.

  • Early (preprocessed) Objective-C had a "programmer knows best" policy, similar to C's malloc/free system. It has no autorelease pool; the programmer frees an object when they know it's done with.
  • Foundation introduced[*] reference-counted memory management, and used the autorelease pool to implement deferred releasing. This is a "object's programmer knows best" policy where each object can say what it owns but there's no holistic memory management coding needed.
  • Garbage-collected Objective-C exists in at least two (possibly three) incarnations. Even in Garbage-collected Foundation there are autorelease pools, but there's a "runtime knows best" policy so you could get away without an autorelease pool because the runtime can see when an object is still in use.
  • Automatic reference counting, as its name suggests, automates Foundation's reference-counted memory management scheme. In theory you don't need an autorelease pool because the compiler and runtime can tell how an object gets used and fix up ownership. In practice you still do because you can interoperate with manually reference-counted code. ARC is still "object's programmer knows best", but it drops manual retains and releases in favour of manual strong/weak/unsafe reference tagging.

Your suggestion that an object should be retained only when it's aliased to a named variable visible to the compiler will not work in the general case. The programmer could keep an unnamed reference to the object (e.g. in a collection or via an association), or they could create an object that must stay around despite there being no alias to it.

[*] In fact the reference-counted system existed in Mach Kit as NXReference (and was used in other frameworks, like Indexing Kit) earlier, but when it became part of Foundation it became conventional for all Objective-C classes (in NeXTStep and WO applications, and hence in Mac and iOS apps) to use reference-counted memory management.

5
  • Not all Core Foundation objects return caller-owned objects, e.g. CFAttributedStringGetString etc.
    – dreamlax
    Apr 10, 2013 at 13:17
  • Very nice overview. But still doesn't explain why they chose the autorelease-pool concept (where the programmer has to manually add/drain autorelease-pools in order to prevent potential memory bloat in loops) rather than something else (i.e. make the programmer mark method return values as owned/unowned and make the compiler insert a release call to each unowned return value on the next line - no pool needed)
    – Askaga
    Apr 10, 2013 at 14:21
  • @BillAskaga I did answer the questions you asked though :). Are you right about the preprocessor thing? No. Are there other concepts for dealing with it? Yes, listed.
    – user23743
    Apr 10, 2013 at 14:49
  • 1
    @BillAskaga The technique you suggest stops me (like garbage collectors do) from deliberately trading space off against time. Do I want to do something quickly now and can use up some RAM in doing so? OK, I'll put objects I'm done with in a pool instead of releasing them. When the user goes for a coffee in a few minutes, I'll drain the pool because I don't need CPU time then and can reclaim the memory.
    – user23743
    Apr 10, 2013 at 15:02
  • 4
    Reference counted management came as a part of the OpenStep APIs [~1993]; it was used prior internally as Graham indicated. It was chosen to provide a balance between automation and efficiency. Note that it has some significant costs, too. The notion that reference counting is free compared to, say, garbage collection is simply not true (in fact, GC can provide significant performance improvements over reference counting at the cost of determinism).
    – bbum
    Apr 10, 2013 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.