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

NSInvocation+OCMAdditions: don't retain deallocating objects. #11

Merged
merged 1 commit into from
May 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add a comment as to why allowsWeakReference is needed.

{
// 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