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

GULObjectSwizzler: allow to disable generated class dispose calls. #66

Merged
merged 1 commit into from
Jan 6, 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
35 changes: 17 additions & 18 deletions GoogleUtilities/ISASwizzler/GULObjectSwizzler.m
Original file line number Diff line number Diff line change
Expand Up @@ -160,23 +160,25 @@ - (void)swizzle {
}

- (void)dealloc {
// When the Zombies instrument is enabled, a zombie is created for the
// swizzled object upon deallocation. Because this zombie subclasses
// the generated class, the swizzler should not dispose it during the
// swizzler's deallocation. The `zombiesEnabled` value is used to guard calls
// to dispose the generated class.
// When the Zombies instrument is enabled, a zombie is created for the swizzled object upon
// deallocation. Because this zombie subclasses the generated class, the swizzler should not
// dispose it during the swizzler's deallocation.
//
// There are other special cases where the generated class might be subclassed by a third-party
// generated classes, for example: https://github.com/firebase/firebase-ios-sdk/issues/9083
// To avoid errors in such cases, the environment variable `GULGeneratedClassDisposeDisabled` can
// be set with `YES`.
NSDictionary *environment = [[NSProcessInfo processInfo] environment];
BOOL zombiesEnabled = [[environment objectForKey:@"NSZombieEnabled"] boolValue];
if ([[environment objectForKey:@"NSZombieEnabled"] boolValue] ||
[[environment objectForKey:@"GULGeneratedClassDisposeDisabled"] boolValue]) {
return;
}

if (_generatedClass) {
if (_swizzledObject == nil) {
// The swizzled object has been deallocated already, so the generated class can be disposed
// now.

// Do not dispose the generated class if zombies is enabled.
if (!zombiesEnabled) {
objc_disposeClassPair(_generatedClass);
}
objc_disposeClassPair(_generatedClass);
return;
}

Expand All @@ -196,13 +198,10 @@ - (void)dealloc {
if (isSwizzledObjectInstanceOfGeneratedClass) {
Class generatedClass = _generatedClass;

// Do not dispose the generated class if zombies is enabled.
if (!zombiesEnabled) {
// Schedule the generated class disposal after the swizzled object has been deallocated.
dispatch_async(dispatch_get_main_queue(), ^{
objc_disposeClassPair(generatedClass);
});
}
// Schedule the generated class disposal after the swizzled object has been deallocated.
dispatch_async(dispatch_get_main_queue(), ^{
objc_disposeClassPair(generatedClass);
});
}
}
}
Expand Down