troubles with memory leak

Discussion in 'C++' started by =?gb2312?B?uqO35w==?=, Jan 31, 2007.

  1. Hi
    i am using libMagick++.a of Magick-6.2.8 in linux.but now i encounter
    some leaks when
    reading blob into image , writting image into blob, or throwing
    expection .that are my code patch and leak reports in valgrind
    below .thanks for your attention.



    void CFileChange::leak_test(const unsigned long &id, char *name, Blob
    & fileblob)
    {
    if ( NULL == name || strlen(name) >= 256 ){
    printf("filename error! id=%d", id);
    return;
    }

    char nametmp[256] ={0};
    char namef[256]={0};
    char namefex[256]={0};
    string strtmp;
    memcpy(nametmp, name,strlen(name));
    char * post = strrchr(nametmp,'.');
    if ( NULL == post)
    {
    printf("filename error! name=%s id=%d", name, id);

    for ( vector<DIR_ITEM>::iterator iter = m_dirs.begin();
    iter != m_dirs.end(); ++iter)
    {
    DIR_ITEM item = (DIR_ITEM)*iter;
    strtmp.clear();
    strtmp = m_dec;
    strtmp.append("/");
    strtmp.append(item.dir_path);
    strtmp.append("/");
    strtmp.append(name);

    FILE *fd = fopen(strtmp.c_str(),"wb");
    if ( NULL != fd)
    fclose(fd);
    }

    return;
    }

    *post = '\0';
    post++;
    if ( NULL == post || (*post) == '\0')
    {
    printf("filename error! name=%s id=%d", name, id);

    for ( vector<DIR_ITEM>::iterator iter = m_dirs.begin();
    iter != m_dirs.end(); ++iter)
    {
    DIR_ITEM item = (DIR_ITEM)*iter;
    strtmp.clear();
    strtmp = m_dec;
    strtmp.append("/");
    strtmp.append(item.dir_path);
    strtmp.append("/");
    strtmp.append(name);

    FILE *fd = fopen(strtmp.c_str(),"wb");
    if ( NULL != fd)
    fclose(fd);
    }

    }
    memcpy(namefex,post,strlen(post));
    memcpy(namef,nametmp,strlen(nametmp));

    if ( 0 == fileblob.length())
    {
    CreateFile(id, name);
    printf("file empty error! name=%s id=%d \n", name, id);
    return;

    }


    if ( 0 != strcmp("jpg",namefex) )
    return;


    Image image; //line 594
    Blob blob;
    Image second;

    try
    {

    try{
    image.read(fileblob);

    unsigned int width = image.baseColumns();
    unsigned int high = image.baseRows();
    bool bmin = false;

    if (width > 100 || high > 100)
    {
    bmin = true;
    }

    image.profile("*",blob);
    image.write(&blob); //line 614

    second.magick("JPEG");
    second.quality(m_imgquality);
    second.read(blob);
    if ( false != bmin)
    second.zoom(Geometry(100,100));


    }
    catch( Magick::WarningCoder &warning )
    {
    Log::addStr("coder file_warning %s ! name=%s id=%d
    \n",warning.what(), name,id);

    }
    catch( Magick::Warning &warning )
    {
    Log::addStr(" file_warning %s ! name=%s id=%d
    \n",warning.what(), name,id);
    return;

    }
    catch( Magick::ErrorFileOpen &error )
    {
    // Process Magick++ file open error
    Log::addStr("open file_error %s ! name=%s id=%d
    \n",error.what(), name,id);
    CreateFile(id, name);
    return; // Try next image.
    }
    catch( Magick::Exception &error )
    {
    // Process Magick++ file open error
    Log::addStr("write file_error %s ! name=%s id=%d
    \n",error.what(), name,id);
    CreateFile(id, name);
    return; // Try next image.
    }


    try{


    strtmp.clear();
    strtmp = m_dec;
    strtmp.append("/");
    strtmp.append("test");
    strtmp.append("/");
    strtmp.append(name);

    second.write(strtmp.c_str());
    struct stat bufstat;
    if ( lstat(strtmp.c_str(),&bufstat) <0)
    {
    printf("get lstat_error name:%s\n", strtmp.c_str());

    return;
    }
    if ( S_ISREG(bufstat.st_mode))
    {
    long len = bufstat.st_size;

    if ( len > m_maxsize)
    {
    Log::addStr("file_too_long, kill it! %ld :%s
    \n",len,strtmp.c_str());
    //remove(strtmp.c_str());
    }

    }


    }
    catch( Magick::Exception &error )
    {
    // Process Magick++ file open error
    Log::addStr("write file_error %s ! name=%s id=%d
    \n",error.what(), name,id);
    CreateFile(id, name);
    return; // Try next image.
    }


    }
    catch(std::exception &error )
    {
    // Process Magick++ file open error
    Log::addStr("std file_error %s ! name=%s id=%d \n",error.what(),
    name,id);
    CreateFile(id, name);
    return; // Try next image.
    }
    catch(...)
    {
    Log::addStr("final file_error! name=%s id=%d \n", name,id);
    CreateFile(id, name);
    return ;
    }

    return;
    }


    ////////////////////////leaks

    ==6095== 37,040 bytes in 9 blocks are definitely lost in loss record
    34 of 40
    ==6095== at 0x401A639: malloc (vg_replace_malloc.c:149)
    ==6095== by 0x43AEDA8: AcquireMagickMemory (memory.c:321)
    ==6095== by 0x439A776: NewLinkedList (hashmap.c:1407)
    ==6095== by 0x438D1C8: GetExceptionInfo (exception.c:428)
    ==6095== by 0x40B3933:
    Magick::throwException(MagickLib::_ExceptionInfo&) (in /usr/local/lib/
    libMagick++.so.10.0.3)
    ==6095== by 0x40BDFAB: Magick::Image::write(Magick::Blob*) (in /usr/
    local/lib/libMagick++.so.10.0.3)
    ==6095== by 0x804F513: CFileChange::leak_test(unsigned long const&,
    char*, Magick::Blob&) (swap_picture.cpp:614)

    ==6095== 51,558 (40 direct, 51,518 indirect) bytes in 2 blocks are
    definitely lost in loss record 21 of 40
    ==6095== at 0x401A9D0: operator new(unsigned) (vg_replace_malloc.c:
    164)
    ==6095== by 0x40B9B88: Magick::Image::Image() (in /usr/local/lib/
    libMagick++.so.10.0.3)
    ==6095== by 0x804F384: CFileChange::leak_test(unsigned long const&,
    char*, Magick::Blob&) (swap_picture.cpp:594)

    ==6095== 31,784 (84 direct, 31,700 indirect) bytes in 1 blocks are
    definitely lost in loss record 31 of 40
    ==6095== at 0x401A639: malloc (vg_replace_malloc.c:149)
    ==6095== by 0x465188E: jpeg_get_small (in /usr/lib/libjpeg.so.
    62.0.0)
    ==6095== by 0x4651728: jinit_memory_mgr (in /usr/lib/libjpeg.so.
    62.0.0)
    ==6095== by 0x463963F: jpeg_CreateCompress (in /usr/lib/libjpeg.so.
    62.0.0)
    ==6095== by 0x4CA8231: WriteJPEGImage (jpeg.c:1570)
    ==6095== by 0x434A15B: WriteImage (constitute.c:992)
    ==6095== by 0x43266F9: ImageToBlob (blob.c:1194)
    ==6095== by 0x40BDF99: Magick::Image::write(Magick::Blob*) (in /usr/
    local/lib/libMagick++.so.10.0.3)
    ==6095== by 0x804F513: CFileChange::leak_test(unsigned long const&,
    char*, Magick::Blob&) (swap_picture.cpp:614)
     
    =?gb2312?B?uqO35w==?=, Jan 31, 2007
    #1
    1. Advertising

  2. º£·ç a ¨¦crit :
    > Hi
    > i am using libMagick++.a of Magick-6.2.8 in linux.but now i encounter
    > some leaks when
    > reading blob into image , writting image into blob, or throwing
    > expection .that are my code patch and leak reports in valgrind
    > below .thanks for your attention.
    >
    > [snip]
    >
    > Image image; //line 594
    > Blob blob;
    > Image second;
    >
    > try
    > {
    > [snip]
    > image.write(&blob); //line 614
    > [snip]
    > }
    > catch( [snip...] )
    > [snip]
    > ////////////////////////leaks
    >
    > ==6095== 37,040 bytes in 9 blocks are definitely lost in loss record
    > 34 of 40
    > ==6095== at 0x401A639: malloc (vg_replace_malloc.c:149)
    > [snip]
    > ==6095== by 0x804F513: CFileChange::leak_test(unsigned long const&,
    > char*, Magick::Blob&) (swap_picture.cpp:614)
    >


    Image image; is declared outside the try block and therefore is not
    destroyed upon exception but should be destroyed at the end of the
    function. The reality of the memory leak depends on when valgrind
    stopped its accounting; try to put it in the try block and rerun with
    valgrind.
    Otherwise, it may mean that Image::write was badly designed concerning
    exception handling.

    Michael
     
    Michael DOUBEZ, Jan 31, 2007
    #2
    1. Advertising

  3. First thanks alot. According to your saying, i put image and blob
    into try block . but when expection is thrown, leaks come .
    //

    8 bytes in 1 blocks are definitely lost in loss record 1 of 23
    ==3679== at 0x401A639: malloc (vg_replace_malloc.c:149)
    ==3679== by 0x4172526: __cxa_get_globals (in /usr/lib/libstdc++.so.
    5.0.3)
    ==3679== by 0x417323D: __cxa_throw (in /usr/lib/libstdc++.so.5.0.3)
    ==3679== by 0x40B3F9E:
    Magick::throwException(MagickLib::_ExceptionInfo&) (in /usr/local/lib/
    libMagick++.so.10.0.3)
    ==3679== by 0x40BCBD1: Magick::Image::read(Magick::Blob const&)
    (in /usr/local/lib/libMagick++.so.10.0.3)
    ==3679== by 0x804F3DB: CFileChange::leak_test(unsigned long const&,
    char*, Magick::Blob&) (swap_picture.cpp:601)

    ==3679== 32 bytes in 1 blocks are definitely lost in loss record 4 of
    23
    ==3679== at 0x401A639: malloc (vg_replace_malloc.c:149)
    ==3679== by 0x43AEDA8: AcquireMagickMemory (memory.c:321)
    ==3679== by 0x439A776: NewLinkedList (hashmap.c:1407)
    ==3679== by 0x438D1C8: GetExceptionInfo (exception.c:428)
    ==3679== by 0x40B3933:
    Magick::throwException(MagickLib::_ExceptionInfo&) (in /usr/local/lib/
    libMagick++.so.10.0.3)
    ==3679== by 0x40BCBD1: Magick::Image::read(Magick::Blob const&)
    (in /usr/local/lib/libMagick++.so.10.0.3)
    ==3679== by 0x804F3DB: CFileChange::leak_test(unsigned long const&,
    char*, Magick::Blob&) (swap_picture.cpp:601)



    I guess that the author meas if a exception occurs then the app should
    be over.so the dynamic memory relating to exception is not free
    automatically.
     
    =?gb2312?B?uqO35w==?=, Jan 31, 2007
    #3
  4. º£·ç a ¨¦crit :
    > First thanks alot. According to your saying, i put image and blob
    > into try block . but when expection is thrown, leaks come .
    > //
    >
    > 8 bytes in 1 blocks are definitely lost in loss record 1 of 23
    > ==3679== at 0x401A639: malloc (vg_replace_malloc.c:149)
    > ==3679== by 0x4172526: __cxa_get_globals (in /usr/lib/libstdc++.so.
    > 5.0.3)
    > ==3679== by 0x417323D: __cxa_throw (in /usr/lib/libstdc++.so.5.0.3)
    > ==3679== by 0x40B3F9E:
    > Magick::throwException(MagickLib::_ExceptionInfo&) (in /usr/local/lib/
    > libMagick++.so.10.0.3)
    > ==3679== by 0x40BCBD1: Magick::Image::read(Magick::Blob const&)
    > (in /usr/local/lib/libMagick++.so.10.0.3)
    > ==3679== by 0x804F3DB: CFileChange::leak_test(unsigned long const&,
    > char*, Magick::Blob&) (swap_picture.cpp:601)
    >
    > ==3679== 32 bytes in 1 blocks are definitely lost in loss record 4 of
    > 23
    > ==3679== at 0x401A639: malloc (vg_replace_malloc.c:149)
    > ==3679== by 0x43AEDA8: AcquireMagickMemory (memory.c:321)
    > ==3679== by 0x439A776: NewLinkedList (hashmap.c:1407)
    > ==3679== by 0x438D1C8: GetExceptionInfo (exception.c:428)
    > ==3679== by 0x40B3933:
    > Magick::throwException(MagickLib::_ExceptionInfo&) (in /usr/local/lib/
    > libMagick++.so.10.0.3)
    > ==3679== by 0x40BCBD1: Magick::Image::read(Magick::Blob const&)
    > (in /usr/local/lib/libMagick++.so.10.0.3)
    > ==3679== by 0x804F3DB: CFileChange::leak_test(unsigned long const&,
    > char*, Magick::Blob&) (swap_picture.cpp:601)
    >
    >
    >
    > I guess that the author meas if a exception occurs then the app should
    > be over.so the dynamic memory relating to exception is not free
    > automatically.


    A possibility is that the object thrown has been allocated and you are
    expected to delete it. You should look in the Magick++ manual.

    Otherwise, I have no idea.

    Good luck.
    Michael
     
    Michael DOUBEZ, Jan 31, 2007
    #4
  5. > A possibility is that the object thrown has been allocated and you are
    > expected to delete it. You should look in the Magick++ manual.
    >
    > Otherwise, I have no idea.
    >
    > Good luck.


    Thanks again . i have searched all codes in Magick++ ,but did not
    find any trace about it. Now, i have to kill the process and then
    restart it at stated period only in order to free memory
     
    =?gb2312?B?uqO35w==?=, Feb 2, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Y3liZXJzdHJpa2U=?=

    datagrid memory leak?

    =?Utf-8?B?Y3liZXJzdHJpa2U=?=, Jan 3, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    485
    =?Utf-8?B?Y3liZXJzdHJpa2U=?=
    Jan 3, 2005
  2. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    764
    Eric Sosman
    Mar 22, 2005
  3. Richard Heathfield

    Leak or no leak ??

    Richard Heathfield, Jul 10, 2006, in forum: C Programming
    Replies:
    4
    Views:
    381
    Richard Heathfield
    Jul 10, 2006
  4. cham
    Replies:
    5
    Views:
    794
  5. Mark Probert
    Replies:
    4
    Views:
    357
    Mark Probert
    Feb 9, 2005
Loading...

Share This Page