Do I need to use autorelease on object which created not using alloc init?

51 views Asked by At

I usually work using recent version of Objective C / Swift which already use ARC. But now I have to continuing the work on progress, which uses an old "situation" of Objective C, which happen to predates the ARC stuff. I have never learn about how to use release and autorelease properly, and now got confused and paranoid.

From what I learn, I can just use autorelease on any of my object initialisation, and not worry about the rest of it, just like if I use ARC. But recently, I began to think whether I also need to add autorelease to:

  • object automatically created using syntax, which using @ symbol.
  • object returned from method and static method, such as [UIColor redColor].

So now, I began to add autorelease each time I see any object creation. I even began to write:

NSString * data = [@"" autorelease];
MyClass * myClass = [[[MyClass alloc] init] autorelease];
if (flag == true) {
    data = [[NSString stringWithFormat:[@"%@%@" autorelease], info1, info2, nil] autorelease];
    label.backgroundColor = [[UIColor redColor] autorelease];
}
label.text = data;

Can you give me a simple rule of when I need or don't need to use autorelease, because all the autoreleases in my code starts to make me check my sanity of whether I'm doing the right thing or it's just my paranoia. Help!

0

There are 0 answers