小编use*_*581的帖子

导致此内存泄漏的原因是什么?

内存泄漏发生了.代码有什么问题?

static sigjmp_buf jmpbuf=NULL;

static void alarm_func()  
{  
   siglongjmp(jmpbuf, 1);  
}  
static struct hostent *timeGethostbyname(const char *domain, int timeout)  
{  
    struct hostent *ipHostent = NULL;  
    jmpbuf=malloc(sizeof(sigjmp_buf));
    signal(SIGALRM, alarm_func);  
    if(sigsetjmp(jmpbuf, 1) != 0)  
    {  
        alarm(0);
        signal(SIGALRM, SIG_IGN);  
        return NULL;  
    }  
    alarm(timeout);//setting alarm  
    ipHostent = gethostbyname(domain);  
    signal(SIGALRM, SIG_IGN);  
    return ipHostent;  
} 
Run Code Online (Sandbox Code Playgroud)

功能上有问题timeGethostbyname.如果我timeGethostbyname多次调用该功能.将发生内存泄漏.EX:

   int main(int argc, char **argv ){
        char *servers="www.aaa.bbb.tt";
        struct hostent *h;
        while(1){
            h=timeGethostbyname(servers, 2);
        }

        return(0);

    }
Run Code Online (Sandbox Code Playgroud)

c linux memory-leaks

2
推荐指数
1
解决办法
340
查看次数

标签 统计

c ×1

linux ×1

memory-leaks ×1