Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > troubles with memory leak

Reply
Thread Tools

troubles with memory leak

 
 
=?gb2312?B?uqO35w==?=
Guest
Posts: n/a
 
      01-31-2007
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:42
==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)

 
Reply With Quote
 
 
 
 
Michael DOUBEZ
Guest
Posts: n/a
 
      01-31-2007
º£·ç 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
 
Reply With Quote
 
 
 
 
=?gb2312?B?uqO35w==?=
Guest
Posts: n/a
 
      01-31-2007
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:42
==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.

 
Reply With Quote
 
Michael DOUBEZ
Guest
Posts: n/a
 
      01-31-2007
º£·ç 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:42
> ==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
 
Reply With Quote
 
=?gb2312?B?uqO35w==?=
Guest
Posts: n/a
 
      02-02-2007
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory leak even after deleting memory pointers from vector cham C++ 5 09-25-2008 10:30 AM
Leak or no leak ?? Richard Heathfield C Programming 4 07-10-2006 11:37 AM
usb memory stick troubles The Shed Computer Support 7 10-19-2005 01:05 PM
Dynamic memory allocation and memory leak... s.subbarayan C Programming 10 03-22-2005 02:48 PM
Memory leak??? (top reporting high memory usage under Solaris) Mark Probert Ruby 4 02-09-2005 06:13 PM



Advertisments