Forum Archive

When to call autorelease ?

ywangd

When creating objc objects, how I can tell if autorelease calls are necessary? Python objects are garbage collected. Can I rely on this for objc objects as well?

I looked at sample codes provided by @omz , but cannot find a pattern when to call autorelease.

Specifically, do I need to call autorelease when creating an object that subclass UITextView? And what about NSMutableAttributedString?

omz

As a simple rule of thumb: If you see alloc().init...() or new(), you need to balance it with release() or autorelease() somewhere. Objects that are created by other means (returned from a method that isn't called init...()) are usually already autoreleased.

ywangd

Thanks @omz The pattern seems reasonable to follow.