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

bs_machThreadFromNSThread will cause mach port leak? #18

Open
holybin opened this issue Nov 20, 2021 · 0 comments
Open

bs_machThreadFromNSThread will cause mach port leak? #18

holybin opened this issue Nov 20, 2021 · 0 comments

Comments

@holybin
Copy link

holybin commented Nov 20, 2021

thread_t bs_machThreadFromNSThread(NSThread *nsthread) {
    char name[256];
    mach_msg_type_number_t count;
    thread_act_array_t list;
    task_threads(mach_task_self(), &list, &count);
    
    NSTimeInterval currentTimestamp = [[NSDate date] timeIntervalSince1970];
    NSString *originName = [nsthread name];
    [nsthread setName:[NSString stringWithFormat:@"%f", currentTimestamp]];
    
    if ([nsthread isMainThread]) {
        return (thread_t)main_thread_id;
    }
    
    for (int i = 0; i < count; ++i) {
        pthread_t pt = pthread_from_mach_thread_np(list[i]);
        if ([nsthread isMainThread]) {
            if (list[i] == main_thread_id) {
                return list[i];
            }
        }
        if (pt) {
            name[0] = '\0';
            pthread_getname_np(pt, name, sizeof name);
            if (!strcmp(name, [nsthread name].UTF8String)) {
                [nsthread setName:originName];
                return list[i];
            }
        }
    }
    
    [nsthread setName:originName];
    return pthread_mach_thread_np(pthread_self());
}

this method will cause mach port leak

@holybin holybin changed the title mach_thread_self 的调用会导致mach port泄露 bs_machThreadFromNSThread will cause port leak? Jan 18, 2022
@holybin holybin changed the title bs_machThreadFromNSThread will cause port leak? bs_machThreadFromNSThread will cause mach port leak? Jan 18, 2022
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