Tutorial :static object memory release



Question:

My Questing is

-(ABC*)createInstance  {    Static ABC *obj = [[alloc ABC] init];      if(obj == nil)    {      obj = [[alloc ABC] init];    }    return obj  }  

can we write in objective c

[[ABC createInstance] release]  


Solution:1

Yes, users could release your instance if you haven't overridden retain/release. Take a look at common singleton patterns for better approaches.

Notes:

  • It is [ClassName alloc], not [alloc ClassName]
  • If you meant to make createInstance a class method use + (ABC*)createInstance;
  • It is static, not Static
  • You have to initialize obj to nil
  • Use self instead of ABC if subclassing is a concern: [self alloc]
  • The common name for these methods is sharedInstance

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »