读书人

使用posix_timer编程内存泄露有关问

发布时间: 2012-11-04 10:42:41 作者: rapoo

使用posix_timer编程,内存泄露问题
我使用posix_timer创建定时器,
代码如下:

C/C++ code
#include<stdio.h>#include<time.h>#include<signal.h>#include<pthread.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<iostream>using namespace std;void fun1(sigval_t   v){    printf("122334\n");}timer_t   tid1;typedef void(*pFun)(sigval_t);int SetTimer(pFun pfun,timer_t &tid,int seconds,int id){    struct   sigevent   se;    struct   itimerspec   ts,   ots;    memset(&se,0,sizeof(sigevent));    memset(&ts,0,sizeof(itimerspec));    memset(&ts,0,sizeof(itimerspec));    se.sigev_notify   =   SIGEV_THREAD;    se.sigev_notify_function   =   pfun;    se.sigev_value.sival_int   =   id;    timer_create(CLOCK_REALTIME,&se,&tid);    ts.it_value.tv_sec   =   3;    ts.it_value.tv_nsec   =   0;    ts.it_interval.tv_sec   =   seconds;    ts.it_interval.tv_nsec   =   0;    if(timer_settime(tid,TIMER_ABSTIME,&ts,&ots) < 0)    {        return   -1;    }    return   0;}int main (){    pFun pfun = fun1;    SetTimer(pfun,tid1,13,1);    for(int i =0;i<5;i++ )    {        printf("11111\n");        sleep(1);    }    timer_delete(tid1);    return 1;}



使用valgrind进行内存检测,出现内存泄露情况
如下:
[root@localhost Debug]# valgrind --tool=memcheck --leak-check=yes ./timeUse
==7258== Memcheck, a memory error detector
==7258== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==7258== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==7258== Command: ./timeUse
==7258==
11111
122334
11111
11111
11111
11111
==7258==
==7258== HEAP SUMMARY:
==7258== in use at exit: 144 bytes in 1 blocks
==7258== total heap usage: 4 allocs, 3 frees, 352 bytes allocated
==7258==
==7258== 144 bytes in 1 blocks are possibly lost in loss record 1 of 1
==7258== at 0x4005F0D: calloc (vg_replace_malloc.c:593)
==7258== by 0x389F29: _dl_allocate_tls (in /lib/ld-2.12.so)
==7258== by 0x56B34F: pthread_create@@GLIBC_2.1 (in /lib/libpthread-2.12.so)
==7258== by 0x59D131: __start_helper_thread (in /lib/librt-2.12.so)
==7258== by 0x5708DF: pthread_once (in /lib/libpthread-2.12.so)
==7258== by 0x804875B: SetTimer(void (*)(sigval), void*&, int, int) (main.cpp:38)
==7258== by 0x80487E3: main (main.cpp:55)
==7258==
==7258== LEAK SUMMARY:
==7258== definitely lost: 0 bytes in 0 blocks
==7258== indirectly lost: 0 bytes in 0 blocks
==7258== possibly lost: 144 bytes in 1 blocks
==7258== still reachable: 0 bytes in 0 blocks
==7258== suppressed: 0 bytes in 0 blocks
==7258==
==7258== For counts of detected and suppressed errors, rerun with: -v
==7258== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 17 from 8)
[root@localhost Debug]#

我哪里使用错误?还是valgrind有问题?请指定,谢谢!


[解决办法]
valgrind 没有用过,看起来未释放的是pthread tls内存。

楼主可以试试建立一个只包含空的main函数的程序,然后用valgrind检查看看。
[解决办法]
问题出现在
SetTimer(void (*)(sigval), void*&, int, int) (main.cpp:38)
main (main.cpp:55)

代码中
SetTimer(pfun,tid1,13,1);
typedef void(*pFun)(sigval_t);
[解决办法]
探讨

to:root_jli

这是函数指针
typedef void(*pFun)(sigval_t);
SetTimer(pfun,tid1,13,1);

如何释放?

读书人网 >UNIXLINUX

热点推荐