Objective-c – CALayer addSublayer increasing retain count

cocoa-touchiphoneobjective c

I think that when I add a view as a subview like such:

UIView* view = [[UIView alloc] init];
[self addSubview:view];
[view release];

that it's safe to release the view afterwards…is it the same for a CALayer object? If I create a CALayer using alloc/init, and do:

[self.layer addSublayer:layer];

is it safe to do a release of the layer afterwards?

Best Answer

Yes. In general if object A needs object B it is object A's responsibility to retain it. So if "self.layer" needs "layer, it will bump the retainCount during addSublayer: , and release layer when it is no longer needed. While there are some exceptions to this, those exceptions tend to be very well documented.