I know that this is rarely required to override the alloc
or dealloc
methods,but if required is it possible in iPhone programming?
You can and indeed, you should (if using manual memory management) override dealloc
to release any resources you hold (not forgetting to call [super dealloc]
when finished). Overriding alloc
is possible but, as you say, rarely needed.
[super dealloc]
". It's still possible to override dealloc, but rarely useful. –
Unsex dealloc
is still a good practice in case you need to unregister from notifications or remove gesture recognizers when the instance is about to be deallocated. –
Gretchengrete [super dealloc]
or, more importantly, you don't need to, as ARC is "all over it" on your behalf. –
Bria In general, overriding alloc
is only done when you wish to, eg, allocate an object from a pool of available instances, or perhaps allocate a variable amount of storage for the object based on some external parameter. (In C++ you can access the new
parameters and allocate based on them, but Objective-C does not give you access to the initXXX
parameters.)
I've never attempted any of this, and I suspect that its a bit of a minefield -- you need to study up on the structures and be pretty careful.
As Adam said, you should ALWAYS (in a reference counted environment) override dealloc
if there are any retained objects held by your object.
Update: An interesting thing you can do ... in RedClass or a superclass of it code something like:
+(id)alloc {
if (self == [RedClass class]) {
return [BlueClass alloc];
}
else {
return [super alloc];
}
}
The net result is that whenever you execute [RedClass alloc]
a BlueCLass object will be returned. (NB: Presumably BlueClass is a subclass of RedClass, or things will get seriously mucked up shortly after the object is returned.)
Not saying that it's a good idea to do this, but it's possible (and I don't offhand know of any cases where it wouldn't work reliably for vanilla user-defined classes). And it does have a few possible uses.
Additional note: In some cases one might want to use [self isSubclassOf:[RedClass class]]
rather than ==
(though that has some serious pitfalls).
alloc
, but there are no parameters to alloc
(other than self
and _cmd
), so it's not clear what you're referring to. If you meant information about instances, for example their size, the Objective-C runtime gives you access to anything and everything an implementor would need to know. –
Unsex new
parameters. –
Backfire NSNumber
that have a pool of instances for common values? I understand how [NSNumber numberWithInt:1]
etc would work since we get the param before we alloc, but how does it know to return the same instance with [[NSNumber alloc] initWithInt:1]
? Even with tagged pointers or all the other fancy stuff they do behind the scenes, you're still calling alloc first without any knowledge of the stored value. The only thing I can think is that calling [NSNumber alloc]
returns a single shared instance and then -[NSNumber initWith...]
returns the specific one. Yeah? –
Referee [NSNumber alloc]
returns a shared instance of NSPlaceholderNumber
. +[NSNumber numberWithInt:100]
returns a tagged pointer so it's the same no matter how many times you call it. -[NSNumber initWithInt:100]
returns the same tagged pointer. –
Referee self
value returned from the super's init
routine, vs the value it received on the call. –
Backfire alloc
routine, it's just rarely explicitly used. However, I forget the syntax. –
Backfire malloc(sizeof(TheClass))
(or new
with a char
array, then casting it), but it's simply not done. If there's a specialized syntax which would also allow allocation without initialization on the stack instead of heap, I'd appreciate it if you'd post it if you remember it. –
Hyp new
method on your class. The first parm is the size to be allocated, and you may optionally mirror the constructor parms as additional parms to new
. (But I'm recalling this from about 12 years back, so it's a little fuzzy.) –
Backfire new
operator?! That's nasty. o_o It also still requires a malloc()
inside, and it still invokes the constructor. At least from what I can tell. cprogramming.com/tutorial/operator_new.html Looks like invoking only operator new
is supported, though: #2383430 –
Hyp alloc
. I used it to good effect when designing a set of sparse vector classes for data flow analysis. –
Backfire © 2022 - 2024 — McMap. All rights reserved.
NSObject
should overrideallocWithZone:
rather thanalloc
, and can't overridedealloc
in code that's compiled with ARC. – Unsex