allocWithZone

allocWithZone

Postby rhildreth » Sat Aug 20, 2011 2:09 pm

p. 178 - Why are you using allocWithZone instead of alloc?
rhildreth
 
Posts: 4
Joined: Sat Aug 13, 2011 9:41 pm

Re: allocWithZone

Postby one09jason » Wed Sep 07, 2011 9:17 am

I'd like an answer to this as well. Furthermore, what is the point of calling allocWithZone:NULL ?
one09jason
 
Posts: 5
Joined: Mon Aug 22, 2011 12:07 pm

Re: allocWithZone

Postby JoeConway » Wed Sep 07, 2011 11:33 am

The method alloc is just a dummy method. It's implementation looks like this:
Code: Select all
+ (id)alloc
{
      return [self allocWithZone:nil];
}


allocWithZone: will call the C function, NSAllocateObject. This function is where memory is actually allocated.

So, the goal of this singleton class is to prevent anyone from creating another instance of the class. If we were to override alloc, it would prevent the following:
Code: Select all
id newInstance = [[SomeClass alloc] init];


However, this still leaves the possibility of doing this:
Code: Select all
id newInstance = [[SomeClass allocWithZone:nil] init];


So, we're preventing that from happening. If someone uses alloc, it calls our allocWithZone:. If someone uses allocWithZone:, it predictably calls our allocWithZone:, too. Another programmer can't "jump" our alloc override because of this.
JoeConway
 
Posts: 792
Joined: Tue Apr 20, 2010 11:39 am
Location: Atlanta, GA

Re: allocWithZone

Postby PapaCat » Thu Nov 03, 2011 12:37 pm

When I run Analyze I get the following message for the following method:

Object with +0 retain counts returned to caller where a +1 (owning) retain count is expected
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
// Here we prevent the creation of addiitonal instances
+ (id)allocWithZone:(NSZone *)zone
{
return [self defaultStore];
}
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

I read the author's description of this method on page 178 and 179 and don't understand it completely. However, I suspect I can just ignore this warning.
I'm I correct? I don't need a more detailed explanation at this point, just a yes (for "Don't worry about it) or a no (for "You'd better resolve this warning, buddy) answer.
PapaCat
 
Posts: 10
Joined: Sat May 28, 2011 9:48 pm

Re: allocWithZone

Postby JoeConway » Sat Nov 19, 2011 3:36 pm

Right, you don't have to worry about it. The analyzer is smart to pick it up, but in this case, this is the desired effect.
JoeConway
 
Posts: 792
Joined: Tue Apr 20, 2010 11:39 am
Location: Atlanta, GA

Re: allocWithZone

Postby PapaCat » Sat Nov 19, 2011 3:44 pm

Joe:
Thanks for your reply.
Paul Slapikas (aka PapaCat)
PapaCat
 
Posts: 10
Joined: Sat May 28, 2011 9:48 pm

Re: allocWithZone

Postby cbot209 » Sat Dec 17, 2011 11:42 pm

What the OP brought up got me thinking about any possible memory management issues with PossessionStore. Since a call to +allocWithZone was made in the +defaultStore method, this means PossessionStore is responsible for the instance. How would one go about managing memory for this singleton?

Thanx in advance!
cbot209
 
Posts: 28
Joined: Wed Dec 07, 2011 3:38 pm

Re: allocWithZone

Postby drmark » Sun Sep 09, 2012 8:13 pm

Another programmer can't "jump" our alloc override because of this.


What I don't understand is why using allocWithZone would "jump an alloc override"?
drmark
 
Posts: 10
Joined: Mon Nov 21, 2011 5:24 am

Re: allocWithZone

Postby JoergK » Tue Sep 11, 2012 8:49 am

drmark wrote:
Another programmer can't "jump" our alloc override because of this.


What I don't understand is why using allocWithZone would "jump an alloc override"?


If we override alloc instead of allocWithZone a programmer using our class could still call alloc with zone to bypass our changes.
JoergK
 
Posts: 37
Joined: Sun Aug 19, 2012 4:06 am
Location: Germany


Return to 10. UITableview and UITableViewController

Who is online

Users browsing this forum: No registered users and 2 guests