Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid retaining objects that are being deallocated. #398

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Source/OCMock/NSInvocation+OCMAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ - (void)retainObjectArgumentsExcludingObject:(id)objectToExclude
{
id argument;
[self getArgument:&argument atIndex:index];
if((argument != nil) && (argument != objectToExclude))
if((argument != nil) && (argument != objectToExclude) && !OCMIsDeallocating(argument))
{
if(OCMIsBlockType(argumentType))
{
Expand Down Expand Up @@ -122,7 +122,7 @@ - (void)retainObjectArgumentsExcludingObject:(id)objectToExclude
{
id returnValue;
[self getReturnValue:&returnValue];
if((returnValue != nil) && (returnValue != objectToExclude))
if((returnValue != nil) && (returnValue != objectToExclude) && !OCMIsDeallocating(returnValue))
{
if(OCMIsBlockType(returnType))
{
Expand Down
14 changes: 14 additions & 0 deletions Source/OCMock/OCMFunctions.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ - (void)recordFailureWithDescription:(NSString *)description inFile:(NSString *)
- (void)failWithException:(NSException *)exception;
@end

@interface NSObject(OCMKnownNSObjectMethods)
- (BOOL)_isDeallocating;
@end

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

#pragma mark Functions related to ObjC type system

Expand Down Expand Up @@ -466,3 +474,9 @@ void OCMReportFailure(OCMLocation *loc, NSString *description)
}

}

BOOL OCMIsDeallocating(id anObject)
{
return [anObject _isDeallocating];
}

2 changes: 2 additions & 0 deletions Source/OCMock/OCMFunctionsPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ OCPartialMockObject *OCMGetAssociatedMockForObject(id anObject);

void OCMReportFailure(OCMLocation *loc, NSString *description);

BOOL OCMIsDeallocating(id anObject);

BOOL OCMIsNonEscapingBlock(id block);


Expand Down
3 changes: 2 additions & 1 deletion Source/OCMock/OCMStubRecorder.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#import "OCMBlockCaller.h"
#import "OCMRealObjectForwarder.h"
#import "OCMInvocationStub.h"
#import "OCMFunctionsPrivate.h"


@implementation OCMStubRecorder
Expand Down Expand Up @@ -51,7 +52,7 @@ - (OCMInvocationStub *)stub
- (id)andReturn:(id)anObject
{
id action;
if(anObject == mockObject)
if(anObject == mockObject || OCMIsDeallocating(anObject))
{
action = [[[OCMNonRetainingObjectReturnValueProvider alloc] initWithValue:anObject] autorelease];
}
Expand Down
36 changes: 32 additions & 4 deletions Source/OCMockTests/OCMockObjectRuntimeTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,15 @@ - (NSString *)stringForTypedef:(TypedefString *)string

@interface TestDelegate : NSObject

- (void)go;
- (id)go:(id)sender;

@end

@implementation TestDelegate

- (void)go
- (id)go:(id)sender
{
return sender;
}

@end
Expand All @@ -77,7 +78,24 @@ @implementation TestClassWithDelegate
- (void)run
{
TestDelegate *delegate = self.delegate;
[delegate go];
[delegate go:nil];
}

@end


@interface TestClassThatCallsDelegateOnDealloc : NSObject

@property (nonatomic, weak) TestDelegate *delegate;

@end

@implementation TestClassThatCallsDelegateOnDealloc

- (void)dealloc
{
TestDelegate *delegate = self.delegate;
[delegate go:self];
}

@end
Expand Down Expand Up @@ -313,7 +331,7 @@ - (void)testWeakReferencesShouldStayAround

[object run];

OCMVerify([mockDelegate go]);
OCMVerify([mockDelegate go:nil]);
XCTAssertNotNil(object.delegate, @"Should still have delegate");
}

Expand All @@ -329,6 +347,16 @@ - (void)testDynamicSubclassesShouldBeDisposed
XCTAssertEqual(numClassesBefore, numClassesAfter, @"Should have disposed dynamically generated classes.");
}

- (void)testHandlesCallingMockWithSelfAsArgumentInDealloc
{
// Note that this test will crash on failure.
id mock = [OCMockObject mockForClass:[TestDelegate class]];
[[mock expect] go:OCMOCK_ANY];
TestClassThatCallsDelegateOnDealloc *foo = [[TestClassThatCallsDelegateOnDealloc alloc] init];
foo.delegate = mock;
foo = nil;
[mock verify];
}

- (void)testClassesWithResolveMethodsCanBeMocked
{
Expand Down