[gnomemm] Gnome::Canvas memory leak



Hello,
  I am writing a program that uses Gnome::Canvas for it's custom UI
elements (the timeline, it's a live-proformance MIDI sequencer). I
noticed that it leaked memory and I tracked at least part of it to the
following: Gnome::Canvas items don't delete them selves when the
underlying GTKObject is destroyed. This means that if I create a bunch
of lines in a group then delete the group the line GTKObjects are
destroyed, but the C++ line objects are never deleted. This is a large
problem because my program starts from scratch (by deleting the groups,
since it doesn't track the line objects individually)

I have attached a program that exibits the problem.

-Arthur

-- 
Arthur Peters <amp singingwizard org>
#include <libgnomecanvasmm/canvas.h>
#include <libgnomecanvasmm/line.h>
#include <libgnomecanvasmm/init.h>
#include <gtkmm/main.h>
#include <gtkmm/label.h>
#include <gtkmm/box.h>

#include <string>

#include <gtk/gtk.h>

using namespace std;
using namespace SigC;

class TellLabel : public Gtk::Label
{
  public:
	TellLabel( const std::string& _str )
		: Gtk::Label( _str )
	{
		cout << "TellLabel " << this << " has been created." << endl;
	}

	virtual ~TellLabel()
	{
		cout << "TellLabel " << this << " has been destroyed." << endl;
	}
};

class TellLine : public Gnome::Canvas::Line
{
  public:
	TellLine( Gnome::Canvas::Group& _g, Gnome::Canvas::Points& _p )
		: Gnome::Canvas::Line( _g, _p )
	{
		cout << "TellLine " << this << " has been created." << endl;
	}

	virtual ~TellLine()
	{
		cout << "TellLine " << this << " has been destroyed." << endl;
	}
};

int main(int argc, char* argv[])
{
	Gnome::Canvas::init();
	Gtk::Main xKit(argc, argv);
	
	Gnome::Canvas::Canvas* pxCanvas = new Gnome::Canvas::Canvas();

	/*
	  In this loop a GnomeCanvas Line object is allocated using C++
	  and then the underlying GTKObject is deleted. The C++ wrapper
	  object *is not* deleted, resulting in a memory leak.
	*/
	for( int i=0; i<10; i++ )
	{
		Gnome::Canvas::Points points(2);
		
		points[0] = Gnome::Art::Point( 0, 0 );
		points[1] = Gnome::Art::Point( 10, 100 );
		
		Gnome::Canvas::Line* pxLine = manage( new TellLine( *pxCanvas->root(), points ) );
		
		gtk_object_destroy( GTK_OBJECT( pxLine->gobj() ) );
	}

	Gtk::VBox xBox;

	/*
	  In this loop a GTK Label object is allocated using C++ and then
	  the underlying GTKObject is deleted. The C++ wrapper object *is*
	  deleted.
	*/
	for( int i=0; i<10; i++ )
	{
		Gtk::Label* pxL = manage( new TellLabel( std::string("Hi") ) );
		xBox.pack_start( *pxL );

		gtk_object_destroy( GTK_OBJECT( pxL->gobj() ) );
	}

	/*
	  Shouldn't the C++ object be deleted in both cases?
	*/

}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]