Sign up

Pages: 1 2

Re: Memory leak in Pixbuf ctor/dtor?

This is the latest version I came up with:

To gtkD/src/gdk/Pixbuf.d I added the dtor as suggested by Matheus:

~this()
{
	this.gdkPixbuf = null;
	this.unref();
}

In gtkD/src/gtk/Image.d I inserted a line to setFromPixbuf:

public void setFromPixbuf(Pixbuf pixbuf)
{
	// void gtk_image_set_from_pixbuf (GtkImage *image,  GdkPixbuf *pixbuf);
	if (pixbuf !is null) gtk_image_set_from_pixbuf(gtkImage, null); // this line is new
	gtk_image_set_from_pixbuf(gtkImage, (pixbuf is null) ? null : pixbuf.getPixbufStruct());
}

My cbTimer looks like this now:

private bool cbTimer()
{
	auto filename = this.filenames[this.filenameIdx];
	if (++this.filenameIdx >= this.filenames.length) this.filenameIdx = 0;

	if (this.pixbuf) {
		// must not forget this to avoid memory leak
		this.pixbuf.destroy();
	}
	this.pixbuf = new Pixbuf(filename);
	this.image.setFromPixbuf(this.pixbuf);

	// stop after 19 image switches to avoid out of memory
	return ++this.iteration <= 19;
}

Now the program doesn't leak the Pixbuf buffer any more and there's no assertion failure messages any more.

gtkD's memory management should be fixed, because for me a D wrapper around a C library is exactly about not having to concern about memory management.

There's still some minor peculiar up and down in memory consumption in my real program though, but I didn't investigate yet where it comes from nor whether it grows over all.

Tell me one thing: Are you using getPixbuf() somewhere in your code? If no, how are you displaying the image?

No. I create the Pixbuf from the file and set the Image to use this Pixbuf, just as in the full code in my previous posts or the cbTimer code snippet above. In my real code, there's various operations on the Pixbuf before it's given to the Image.

Diez

Re: Memory leak in Pixbuf ctor/dtor?

On Wed, 05 Nov 2014 04:57:25 GMT, Diez wrote:

gtkD's memory management should be fixed, because for me a D wrapper around a C library is exactly about not having to concern about memory management.

There's still some minor peculiar up and down in memory consumption in my real program though, but I didn't investigate yet where it comes from nor whether it grows over all.

Alright, I think that I found those GObject failures:

GLib-GObject-CRITICAL **: g_object_unref: assertion 'G_IS_OBJECT (object)' failed

In your code after you load the image with Pixbuf you set it to an Image type variable calling this.image.setFromPixbuf(), but then in the next loop interaction you are destroying the Pixbuf which is referenced by the image that you had set before.

To correct this problem I use image.clear() before destroying the Pixbuf!

So this is my drawImg() function with NO LEAKS and NO GOBJECT ERRORS:

void drawImg(Context cr){    
    static bool change = true;
            
    if(!(this.pixbuf is null)){
        this.pixbuf.unref();        
        delete pixbuf;
    }          
    
    auto fimg = change ? "teste.png" : "teste2.png";
    
    this.pixbuf = new Pixbuf(fimg);
    
    this.image.setFromPixbuf(pixbuf);        
    
    // I'm just using the next 3 lines to display on the screen
    auto pb = this.image.getPixbuf();       
    cr.setSourcePixbuf(pb, 0, 0);
    cr.paint();
    
    // THIS IS IMPORTANT TO AVOID GOBJECT ERRORS
    image.clear();
    
    change = !change;
}


Another important thing: I need to take off my dtor in Pixbuf.d:

~this(){
    //this.gdkPixbuf = null;
    //this.unref();
}

And as you can see in the code above I'm handling the unref locally, because in my example I'm using:

auto pb = this.image.getPixbuf()

The getPixbuf() calls the dtor (Maybe Garbage Collector?) causing an error.

I'll check later a way to correct this, maybe looking for any references alive etc.

Matheus.

Re: Memory leak in Pixbuf ctor/dtor?

On 11/04/2014 11:14 PM, Mike Wey wrote:

The GISOBJECT (object) failed error go's away when i change:

...code...


I still need to investigate as to whats going on when using delete.

I found what was causing this. (GISOBJECT (object) failed error)
GtkD stores a weak reference to the D Object in the C struct along with
a callback, to notify use when this weak reference is destroyed, eg when
the C struct is destroyed.

In the code Posted by Diez the D object was destroyed/freed, but the c
struct wasn't destroyed because the GtkImage still hed an reference to it.
So when GtkImage tried to destroy the Pixbuf when a new one was set, it
would "try to" call the callback set by gtkD which failed, causing the
errors.

Re: Memory leak in Pixbuf ctor/dtor?

On Wed, 05 Nov 2014 21:13:45 +0100, Mike Wey wrote:

In the code Posted by Diez the D object was destroyed/freed, but the c struct wasn't destroyed because the GtkImage still hed an reference to it.

So when GtkImage tried to destroy the Pixbuf when a new one was set, it would "try to" call the callback set by gtkD which failed, causing the errors.

I think we nailed the same thing. I made a post before you, and if I call Image.clear() (please see above), It doesn't show those GObject failures.

So I think Image.clear() cleans the reference you said.

Matheus.

Re: Memory leak in Pixbuf ctor/dtor?

On 11/05/2014 07:18 PM, MatheusBN wrote:

I think we nailed the same thing. I made a post before you, and if I call Image.clear() (please see above), It doesn't show those GObject failures.

So I think Image.clear() cleans the reference you said.

Matheus.

Yep, thats the case.

I'm looking into adding the destructor for Pixbuf, There seem to be 20
functions in GtkD where the destructor of the returned Pixbuf would fail.

Re: Memory leak in Pixbuf ctor/dtor?

On Wed, 05 Nov 2014 23:45:00 +0100, Mike Wey wrote:

I'm looking into adding the destructor for Pixbuf, There seem to be 20
functions in GtkD where the destructor of the returned Pixbuf would fail.

Alright! I I'd like to take the moment and congratulate you for effort and awesome work!

Matheus.

Re: Memory leak in Pixbuf ctor/dtor?

Destructor added in:
https://github.com/gtkd-developers/GtkD/commit/288264effa8acce1758895fc98ad6e024189092b

Pages: 1 2