Counting owners with a bare ivar (and why a `retain` property ruins it)
You have one underlying resource (say an OpenGL texture) that several wrapper objects may reference across several owners. The moment the last owner goes away, the resource must be really destroyed; while any other owner remains, it must merely be forgotten by the local bookkeeping and left alive.
The classic shortcut is to let the resource handle be its own refcount:
1@interface CGSharedTexture : NSObject
2@property( readonly, assign) GLuint gpuTexture;
3@end
Each wrapper that references the texture keeps one retain on a single shared
CGSharedTexture object. Then "do I still have an owner?" is just:
1if( [_sharedTexture retainCount] > 1) { ...still shared, forget... }
2else { ...last owner, delete... }
This works — as long as the ivar holding that object is never handed out
through anything that retains or autoreleases it. That "as long as" is where
@property( retain) quietly breaks everything.
Why a retain property is a fail #
Consider the innocent-looking property:
1@property( readonly, retain) CGSharedTexture *sharedTexture;
A retain property getter is synthesized roughly as:
1- (CGSharedTexture *) sharedTexture
2{
3 return( [[_sharedTexture retain] autorelease]);
4}
Every read now takes a temporary extra reference and schedules its release for pool drain. So a line like:
1- (CGSharedTexture *) ownerCheck
2{
3 CGSharedTexture *t = [self sharedTexture]; // retainCount just went 1 -> 2
4 ...
5}
...makes an otherwise single-owned texture report retainCount == 2 while t
is alive. If your delete-vs-forget decision reads the count during that window,
it silently takes the wrong branch:
1- (void) didRemoveFromContext:(CGContext *) context
2{
3 // WRONG when the count was inflated by a retained getter or any
4 // borrow that autoreleases:
5 if( [_sharedTexture retainCount] > 1)
6 [context forgetTextureID:_textureID]; // leak! GL storage never freed
7 else
8 [context deleteTextureID:_textureID]; // correct only when count is true
9}
The failure is nasty because it is timing dependent — it depends on whether the extra autoreleased retain happens to be alive when you test. It also conflates two different questions: "do I own this object?" (a refcount question) and "does another context still need this texture?" (an ownership question that the refcount was only standing in for).
Do it with a controlled ivar #
Declare the state as an ivar and manage the count yourself. Ivar access never retains, so the refcount stays exactly equal to the number of real owners.
1@interface CGImage : NSObject < NSObject>
2{
3 CGSharedTexture *_sharedTexture; // NOT a property; see below
4}
5@end
Rules that keep the count usable:
- Only this class creates the shared handle.
- Every wrapper that co-owns the resource takes exactly one
-retainon the same handle, and gives it back with exactly one-release. This should be handled by the class code. - Nothing else — no getter, no dot-syntax, no debug logging argument — ever hands out a retained reference to it.
1// creating the first owner
2_sharedTexture = [[CGSharedTexture alloc] initWithGPUTexture:gpuTexture]; // count 1
3
4// joining from another wrapper (another context, another thread)
5_sharedTexture = [existingSharedTexture retain]; // count 2
The delete-vs-forget decision is then trustworthy, because nobody can perturb the count between the test and the release:
1- (void) didRemoveFromContext:(CGContext *) context
2{
3 if( [_sharedTexture retainCount] > 1)
4 [context forgetTextureID:_textureID]; // others still own it
5 else
6 [context deleteTextureID:_textureID]; // last owner: really delete
7
8 [_sharedTexture release]; // ours; count drops by one
9 _sharedTexture = nil;
10}
Don't add a getter back "just for convenience" #
If you need to read the handle's data (e.g. the underlying GL id) from inside the class, read the ivar directly:
1GLuint gpuTexture = [_sharedTexture gpuTexture]; // ivar, no retain
TL;DR #
- Use a bare ivar for a handle whose
retainCountyou intend to count owners with. - A
retainproperty getter adds a transient retain+autorelease and makes the count lie; the resulting bug is timing dependent and silent. - Keep exactly one retain per real owner on the same object, give it back once, and never expose a retaining accessor.