detect memory leak
- From: ndo rockwellcollins com
- To: gtk-list gnome org
- Subject: detect memory leak
- Date: Tue, 31 Oct 2006 09:31:18 -0800
I've used Gtkmm2.8.3 version. The following
code segment cause a huge memory leak. Can anyone please take a look and
advice me what was wrong with it. My application is not a gtk window application
but it just need to use the GDK level api to manipulate bitmap and save
it out to disk. I turned on mtrace and it showed the leak is in X11.so.
#include <cstdlib>
#include <ctime>
#include <sys/signal.h>
#include <iostream>
#include <gdkmm.h>
#include <unistd.h>
#include <string>
#include <stdexcept>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <mcheck.h>
using namespace Glib;
using namespace Gdk;
using namespace std;
void initSigTest();
void cleanup();
void SigHandler(int sigcode);
bool gShutdown = false;
int main(int argc, char *argv[])
{
mtrace();
std::cout
<< "main Enter" << std::endl;
int
exitCode = EXIT_SUCCESS;
try
{
initSigTest();
Glib::thread_init();
if(Glib::thread_supported())
{
gdk_threads_init();
}
gdk_init(0, NULL);
Pango::init();
Gdk::wrap_init();
int loop = 0;
GError* gerror = NULL;
gchar *contents = NULL;
gsize size;
if(!g_file_get_contents("/home/dev/asxi/asxmaps/AsxTopoBordersOceanTerrain/World_640x320.bmp",
&contents, &size, &gerror))
{
cout <<
"MemLeakTest() g_file_get_contents error=" << gerror->message
<< endl;
}
else
{
while(!gShutdown
)
{
cout << "MemLeakTest() loop="
<< loop++ << endl;
try
{
Glib::RefPtr<Gdk::PixbufLoader>
bloader = PixbufLoader::create("bmp");
bloader->write((guint8*)contents,
size);
cout <<
"MemLeakTes(1)" << endl;
Glib::RefPtr<Gdk::Pixbuf>
gdkpb = bloader->get_pixbuf();
cout <<
"MemLeakTest(2)" << endl;
try
{
cout << "MemLeakTest(3)" <<
endl;
bloader->close();
}
catch(Gdk::PixbufError&
e)
{
cout << "MemLeakTest(4) PixbufError
Exception=" << e.what() << ",Code=" <<
e.code() << endl;
}
Glib::RefPtr<Gdk::Pixbuf>
gdkpbsaved(NULL);
{
Glib::RefPtr<Gdk::Pixmap> gdkpm(NULL);
Glib::RefPtr<Gdk::Bitmap> gdkbmm(NULL);
gdkpb->render_pixmap_and_mask(gdkpm, gdkbmm,
0);
int width, height;
gdkpm->get_size(width, height);
cout << "MemLeakTest(6) width="
<< width << ",height=" << height << endl;
Glib::RefPtr<Gdk::Colormap> gdksavedclrmap
= gdkpm->get_colormap();
Glib::RefPtr<Drawable> drawable = gdkpm;
cout << "MemLeakTest(7)" <<
endl;
gdkpbsaved = Gdk::Pixbuf::create(drawable,
gdksavedclrmap, 0, 0, 0, 0, width, height);
}
cout <<
"MemLeakTest(8)" << endl;
char fname[255];
sprintf(fname,
"/home/output/en/gdktest_%d.jpg", loop);
gdkpbsaved->save(fname,
"jpeg");
cout <<
"MemLeakTest(10)" << endl;
}
catch(...)
{
cout <<
"MemLeakTest::StartGdk(20) Exception" << endl;
}
}
}
if(contents)
{
cout <<
"MemLeakTest(12)" << endl;
g_free(contents);
}
}
catch(std::exception
e)
{
cout << "main exception="
<< e.what() << endl;
}
cleanup();
std::cout
<< "main Exit" << std::endl;
return(exitCode);
}
void initSigTest()
{
signal(SIGINT,
SigHandler);
signal(SIGHUP,
SigHandler);
signal(SIGTERM,
SigHandler);
}
void cleanup()
{
cout
<< "main::cleanup() Enter" << endl;
Glib::wrap_register_cleanup();
Glib::Error::register_cleanup();
gdk_exit(0);
cout
<< "main::cleanup() Leave" << endl;
}
void SigHandler(int sigcode)
{
std::cout
<< "SigHandler() Enter sigcode=" << sigcode <<
std::endl;
gShutdown
= true;
}
[
Date Prev][Date Next] [
Thread Prev][Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]