Skip to content

Commit 27c0810

Browse files
committed
temp change to use objc_weak calls. use squash before submitting
1 parent 7b631d1 commit 27c0810

6 files changed

+31
-12
lines changed

Source/OCMock/NSInvocation+OCMAdditions.m

+5-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#import "NSInvocation+OCMAdditions.h"
2121
#import "OCMFunctionsPrivate.h"
2222
#import "NSMethodSignature+OCMAdditions.h"
23-
#import "NSObject+OCMAdditions.h"
23+
2424

2525
#if (TARGET_OS_OSX && (!defined(__MAC_10_10) || __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_10)) || \
2626
(TARGET_OS_IPHONE && (!defined(__IPHONE_8_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0))
@@ -30,6 +30,7 @@ static BOOL OCMObjectIsClass(id object) {
3030
#define object_isClass OCMObjectIsClass
3131
#endif
3232

33+
3334
@implementation NSInvocation(OCMAdditions)
3435

3536
+ (NSInvocation *)invocationForBlock:(id)block withArguments:(NSArray *)arguments
@@ -65,7 +66,7 @@ - (void)retainObjectArgumentsExcludingObject:(id)objectToExclude
6566
NSMutableArray *retainedArguments = [[NSMutableArray alloc] init];
6667

6768
id target = [self target];
68-
if((target != nil) && (target != objectToExclude) && !object_isClass(target) && ![target _isDeallocating])
69+
if((target != nil) && (target != objectToExclude) && !object_isClass(target) && !OCMIsDeallocating(target))
6970
{
7071
// Bad things will happen if the target is a block since it's not being
7172
// copied. There isn't a very good way to tell if an invocation's target
@@ -83,7 +84,7 @@ - (void)retainObjectArgumentsExcludingObject:(id)objectToExclude
8384
{
8485
id argument;
8586
[self getArgument:&argument atIndex:index];
86-
if((argument != nil) && (argument != objectToExclude) && ![argument _isDeallocating])
87+
if((argument != nil) && (argument != objectToExclude) && !OCMIsDeallocating(argument))
8788
{
8889
if(OCMIsBlockType(argumentType))
8990
{
@@ -105,7 +106,7 @@ - (void)retainObjectArgumentsExcludingObject:(id)objectToExclude
105106
{
106107
id returnValue;
107108
[self getReturnValue:&returnValue];
108-
if((returnValue != nil) && (returnValue != objectToExclude) && ![returnValue _isDeallocating])
109+
if((returnValue != nil) && (returnValue != objectToExclude) && !OCMIsDeallocating(returnValue))
109110
{
110111
if(OCMIsBlockType(returnType))
111112
{

Source/OCMock/NSObject+OCMAdditions.h

-6
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,3 @@
2222
+ (void)enumerateMethodsInClass:(Class)aClass usingBlock:(void (^)(Class cls, SEL sel))aBlock;
2323

2424
@end
25-
26-
27-
@interface NSObject (Internals)
28-
// Return YES if an object is in the process of being deallocated.
29-
- (BOOL)_isDeallocating;
30-
@end

Source/OCMock/OCMFunctions.m

+21
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ - (void)recordFailureWithDescription:(NSString *)description inFile:(NSString *)
3232
- (void)failWithException:(NSException *)exception;
3333
@end
3434

35+
// From objc/runtime/objc-internal.h
36+
// Only available on macOS 10.11/iOS 9.
37+
extern id objc_initWeakOrNil(id *location, id newObj) __attribute__((weak_import));
38+
extern void objc_destroyWeak(id _Nullable * _Nonnull location) __attribute__((weak_import));
3539

3640
#pragma mark Functions related to ObjC type system
3741

@@ -406,3 +410,20 @@ void OCMReportFailure(OCMLocation *loc, NSString *description)
406410
}
407411

408412
}
413+
414+
BOOL OCMIsDeallocating(id anObject)
415+
{
416+
if (!objc_initWeakOrNil)
417+
{
418+
// Pre iOS9/macOS10.11 we just assume all is well since we can't check.
419+
return NO;
420+
}
421+
id deallocatingObject = nil;
422+
BOOL isDeallocating = objc_initWeakOrNil(&deallocatingObject, anObject) == nil;
423+
if (deallocatingObject)
424+
{
425+
objc_destroyWeak(&deallocatingObject);
426+
}
427+
return isDeallocating;
428+
}
429+

Source/OCMock/OCMFunctionsPrivate.h

+2
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,5 @@ void OCMSetAssociatedMockForObject(OCClassMockObject *mock, id anObject);
4040
OCPartialMockObject *OCMGetAssociatedMockForObject(id anObject);
4141

4242
void OCMReportFailure(OCMLocation *loc, NSString *description);
43+
44+
BOOL OCMIsDeallocating(id anObject);

Source/OCMock/OCMStubRecorder.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#import "OCMRealObjectForwarder.h"
2626
#import "OCMFunctions.h"
2727
#import "OCMInvocationStub.h"
28-
#import "NSObject+OCMAdditions.h"
28+
#import "OCMFunctionsPrivate.h"
2929

3030

3131
@implementation OCMStubRecorder
@@ -53,7 +53,7 @@ - (OCMInvocationStub *)stub
5353
- (id)andReturn:(id)anObject
5454
{
5555
id action;
56-
if(anObject == mockObject || [anObject _isDeallocating])
56+
if(anObject == mockObject || OCMIsDeallocating(anObject))
5757
{
5858
action = [[[OCMNonRetainingObjectReturnValueProvider alloc] initWithValue:anObject] autorelease];
5959
}

Source/OCMockTests/OCMockObjectPartialMocksTests.m

+1
Original file line numberDiff line numberDiff line change
@@ -608,4 +608,5 @@ - (void)testMethodSwizzlingWorksForVoidReturns
608608
XCTAssertNoThrow([foo method1], @"Should have worked.");
609609
}
610610

611+
611612
@end

0 commit comments

Comments
 (0)