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

Crash when hooking dispatch_source_t #9

Open
623637646 opened this issue Jan 8, 2022 · 1 comment
Open

Crash when hooking dispatch_source_t #9

623637646 opened this issue Jan 8, 2022 · 1 comment

Comments

@623637646
Copy link
Owner

Crash demo code:

#import "ViewController.h"
#import <Foundation/Foundation.h>
@import EasySwiftHook;

@interface ViewController ()
@property (nonatomic, strong) dispatch_source_t timer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self startTimer];
}

#pragma mark - timer

- (void)startTimer
{
    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
    dispatch_source_set_timer(self.timer, dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), 1 * NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(self.timer, ^{
        NSLog(@"aaa");
    });
    dispatch_resume(self.timer);
    
    
    NSError *error = nil;
    [self.timer sh_hookDeallocAfterAndReturnError:&error closure:^{
        NSLog(@"dealloc");
    }];
    
    NSAssert(error == nil, @"!!!");
}

@end

Crash screenshot:

Screenshot 2022-01-08 at 3 25 39 PM

@623637646
Copy link
Owner Author

Actually, this is a bug from KVO.
The code below also crashes.

#import "ViewController.h"
#import <Foundation/Foundation.h>

@interface ViewController ()
@property (nonatomic, strong) dispatch_source_t timer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self startTimer];
}

#pragma mark - timer

- (void)startTimer
{
    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
    dispatch_source_set_timer(self.timer, dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), 1 * NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(self.timer, ^{
        NSLog(@"aaa");
    });
    dispatch_resume(self.timer);
    [self.timer addObserver:self forKeyPath:@"hash" options:NSKeyValueObservingOptionNew context:NULL];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    NSLog(@"%@", keyPath);
}

@end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant