Skip to content

Commit

Permalink
NSInvocation+OCMAdditions: don't retain deallocating objects.
Browse files Browse the repository at this point in the history
Solves erikdoe#346.
`allowsWeakReference` returns `NO` if the receiver is during
deallocation. Not retaining deallocating objects would prevent crashing
later when the retained arguments are released - including the
previously deallocated objects.
There are some classes that can never have weak references - these would
also not be retained which might limit some OCMock functionality.
  • Loading branch information
ashdnazg committed May 19, 2022
1 parent 0f2f5a8 commit 6c771c4
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions Source/OCMock/NSInvocation+OCMAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ - (void)retainObjectArgumentsExcludingObject:(id)objectToExclude
NSMutableArray *retainedArguments = [[NSMutableArray alloc] init];

id target = [self target];
if((target != nil) && (target != objectToExclude) && !object_isClass(target))
// We don't want to retain a currently deallocating target, we check this by calling
// allowsWeakReference which returns NO in exactly this case.
if((target != nil) && (target != objectToExclude) && !object_isClass(target) &&
[target allowsWeakReference])
{
// Bad things will happen if the target is a block since it's not being
// copied. There isn't a very good way to tell if an invocation's target
Expand Down Expand Up @@ -109,7 +112,9 @@ - (void)retainObjectArgumentsExcludingObject:(id)objectToExclude
// case do not retain the argument. Note: Even though the type is class the
// argument could be a non-class, e.g. an instance of OCMArg.
}
else
// We don't want to retain a currently deallocating object, we check this by calling
// allowsWeakReference which returns NO in exactly this case.
else if([argument allowsWeakReference])
{
[retainedArguments addObject:argument];
}
Expand Down

0 comments on commit 6c771c4

Please sign in to comment.