?
=?gb2312?B?uqO35w==?=
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)
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)