Oolite
Loading...
Searching...
No Matches
ReleaseLockProxy.m
Go to the documentation of this file.
1/* ReleaseLockProxy.h
2 By Jens Ayton
3 This code is hereby placed in the public domain.
4*/
5
6#import "ReleaseLockProxy.h"
7
8#define VERBOSE 0
9
10#if VERBOSE
11#define VerboseLog NSLog
12#else
13#define VerboseLog(...) \
14 do { \
15 } while (0)
16#endif
17
19
20// These exist purely to provide NSMethodSignatures for -[ReleaseLockProxy methodSignatureForSelector:].
21+ (id)initWithObject:(id<NSObject>)object name:(NSString*)name;
22+ (id)initWithRetainedObject:(id<NSObject>)object name:(NSString*)name;
23+ (void)rlpAllowRelease;
24+ (NSString*)rlpObjectDescription;
25
26@end
27
28@implementation ReleaseLockProxy
29
30// *** Boilerplate
31
32+ (id)proxyWithObject:(id<NSObject>)object name:(NSString*)name
33{
34 return [[[self alloc] initWithObject:object name:name] autorelease];
35}
36
37+ (id)proxyWithRetainedObject:(id<NSObject>)object name:(NSString*)name
38{
39 return [[[self alloc] initWithRetainedObject:object name:name] autorelease];
40}
41
42- (id)initWithObject:(id<NSObject>)object name:(NSString*)name
43{
44 return [self initWithRetainedObject:[object retain] name:name];
45}
46
47- (id)initWithRetainedObject:(id<NSObject>)object name:(NSString*)name
48{
49 if (object == nil) {
50 NSLog(@"** ReleaseLockProxy: passed nil object, returning nil proxy.");
51 [self release];
52 return nil;
53 }
54
55 // No super init for proxies.
56
57 _object = object;
58 _name = [name copy];
59 _locked = YES;
60
61 return self;
62}
63
64- (void)dealloc
65{
66 if (_locked) {
67 NSLog(@"** ReleaseLockProxy (%@): deallocated while locked. This shouldn't happen, unless -dealloc is being called directly.", [self rlpObjectDescription]);
68 } else {
69 VerboseLog(@"-- ReleaseLockProxy (%@): deallocated while not locked.", [self rlpObjectDescription]);
70 }
71
72 [_object release];
73 [_name release];
74
75 [super dealloc];
76}
77
79{
80 _locked = NO;
81}
82
84{
85 return _name ? _name : [_object description];
86}
87
88// *** Core functionality
89
90- (void)release
91{
92 unsigned retainCount = [self retainCount];
93
94 if (_locked && retainCount == 1) {
95 // Breakpoint here to catch what would otherwise be the last release before crashing.
96 NSLog(@"** ReleaseLockProxy (%@): released while locked and retain count is one - intercepting retain. Something is broken.", [self rlpObjectDescription]);
97 return;
98 }
99
100 if (_locked) {
101 VerboseLog(@"-- ReleaseLockProxy (%@): released while locked, but retain count > 1; retain count going from %u to %u.", [self rlpObjectDescription], retainCount, retainCount - 1);
102 } else {
103 VerboseLog(@"-- ReleaseLockProxy (%@): released while not locked; retain count going from %u to %u.", [self rlpObjectDescription], retainCount, retainCount - 1);
104 }
105
106 [super release];
107}
108
110{
111 VerboseLog(@"-- ReleaseLockProxy (%@): autoreleased while %slocked and retain count at %u.", [self rlpObjectDescription], _locked ? "" : "not ", [self retainCount]);
112 return [super autorelease];
113}
114
115// *** Proxy stuff.
116
117- (void)forwardInvocation:(NSInvocation*)invocation
118{
119 [invocation invokeWithTarget:_object];
120}
121
122- (NSMethodSignature*)methodSignatureForSelector:(SEL)selector
123{
124 NSMethodSignature* result = nil;
125
126 if (selector == @selector(initWithObject:name:) || selector == @selector(initWithRetainedObject:name:) || selector == @selector(rlpAllowRelease) || selector == @selector(rlpObjectDescription)) {
127 result = [ReleaseLockProxy_SignatureTemplateClass methodSignatureForSelector:selector];
128 } else {
129 result = [(id)_object methodSignatureForSelector:selector];
130 }
131
132 return result;
133}
134
135+ (BOOL)instancesRespondToSelector:(SEL)selector
136{
137 if (selector == @selector(initWithObject:name:) || selector == @selector(initWithRetainedObject:name:) || selector == @selector(rlpAllowRelease) || selector == @selector(rlpObjectDescription)) {
138 return YES;
139 } else {
140 return NO;
141 }
142}
143
144- (BOOL)respondsToSelector:(SEL)selector
145{
147}
148
149@end
150
152
153+ (id)initWithObject:(id<NSObject>)object name:(NSString*)name
154{
155 return nil;
156}
157+ (id)initWithRetainedObject:(id<NSObject>)object name:(NSString*)name
158{
159 return nil;
160}
162{
163}
165{
166 return nil;
167}
168
169@end
#define NSLog(format,...)
Definition OOLogging.h:148
return nil
#define VerboseLog(...)
BOOL instancesRespondToSelector:(SEL selector)
NSString * rlpObjectDescription()
id initWithRetainedObject:name:(id< NSObject > object,[name] NSString *name)
id< NSObject > _object