What is the difference between releasing and autoreleasing?

后端 未结 4 1067
北海茫月
北海茫月 2020-12-08 12:10

I still have some unclear understand about release and autorelease. What are the difference between both of them? I have this code. For facebook connection. I crash it somet

4条回答
  •  無奈伤痛
    2020-12-08 12:45

    background discussion:

    objective-c is reference counted, so objects are deleted when the reference count reaches 0. release reduces the reference-count immediately, autorelease reduces it when the autorelease-pool is popped

    when to use:

    use autorelease when allocating the object if

    • you do not need it after the current function
    • it will be retiained by some other objet/function and will be released by a later by the retaining code
    • when the logic of the current function is tricky, so you would have to send release in a dozen different places before doing a return

    use "manual" release

    • to revert a previous retain (in case you are implementing a library)
    • if you need precise control of freeing objects (e.g. they use lots of memory or the autorelease pool will not be popped for some time)

    but really my freand:

    • read the Memory Management Programming Guide for Cocoa as suggested by Barry and run your code with instruments (zombies and leaks) often to catch any and almost all memory management errors.

    Erik

提交回复
热议问题