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

获取DNS服务2处内存泄露 #24

Open
xxfenxx opened this issue Apr 8, 2018 · 2 comments
Open

获取DNS服务2处内存泄露 #24

xxfenxx opened this issue Apr 8, 2018 · 2 comments

Comments

@xxfenxx
Copy link

xxfenxx commented Apr 8, 2018

res_state res = malloc(sizeof(struct __res_state));
int result = res_ninit(res); // 内存泄露1, 使用leaks检测发现有内存泄露问题,是什么原因?

NSMutableArray *servers = [[NSMutableArray alloc] init];
if (result == 0) {
    union res_9_sockaddr_union *addr_union = malloc(res->nscount * sizeof(union res_9_sockaddr_union)); // 内存泄露2
    res_getservers(res, addr_union, res->nscount);
    
    for (int i = 0; i < res->nscount; i++) {
        if (addr_union[i].sin.sin_family == AF_INET) {
            char ip[INET_ADDRSTRLEN];
            inet_ntop(AF_INET, &(addr_union[i].sin.sin_addr), ip, INET_ADDRSTRLEN);
            NSString *dnsIP = [NSString stringWithUTF8String:ip];
            [servers addObject:dnsIP];
        } else if (addr_union[i].sin6.sin6_family == AF_INET6) {
            char ip[INET6_ADDRSTRLEN];
            inet_ntop(AF_INET6, &(addr_union[i].sin6.sin6_addr), ip, INET6_ADDRSTRLEN);
            NSString *dnsIP = [NSString stringWithUTF8String:ip];
            [servers addObject:dnsIP];
        }
    }
    free(addr_union); // fix: 内存泄露2
}

res_nclose(res);
free(res);
@xxfenxx xxfenxx changed the title 获取DNS内存泄露 获取DNS2处内存泄露 Apr 8, 2018
@xxfenxx xxfenxx changed the title 获取DNS2处内存泄露 获取DNS服务2处内存泄露 Apr 8, 2018
@TuDoudong
Copy link

大佬,知道什么原因了么

@xxfenxx
Copy link
Author

xxfenxx commented Oct 11, 2022 via email

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

2 participants