scorninpc/php-gtk3

g_object_unref or how to destroy objects created?

Closed this issue · 7 comments

I have following code to make custom word wrap width calculation using markup dimensions:

    // iterated
    public static function width(
        string $markup
    ): ?int
    {
        $layout = new PangoLayout(
            (new GtkDrawingArea)->create_pango_context()
        );

        $layout->set_markup(
            $markup, -1
        );

        if ($size = $layout->get_pixel_size())
        {
            return $size['width'];
        }

        return null;
    }

I'm worried, this static method may create lot of objects in memory and not sure php able to destroy them after use.

So, found g_object_unref function that able to destroy PangoLayout but seems this method not implemented yet.
I want to destroy GtkDrawingArea also, with php-gtk API and without destroy entire the app launched.

Is any ideas how to make it properly?

you dont need worrie!

first, g_object_unref will destroy undef gpointer, not php object. this method may need to implemented to use in another situations

and php can handle this https://www.php.net/manual/en/features.gc.php

first, g_object_unref will destroy undef gpointer, not php object.

I meant destroy exactly gpointer stored somewhere on background, not php object allocated.

e.g. emit some signal to Gtk/Pango to kill this one after php operation complete.

This my static method creates new object for every word in string, until calculate entire document.
Paranoia just, because php clean up the object on complete, but gkt may keep thousands of PangoLayout generated.

Are you sure that no needs to care with destroy it manually?

php-gtk is the bridge to cpp api, it re-transmit to gtk and keep data sent there as is, until maybe Gtk::main_quit(), or g_object_unref or widget->destroy emitted.

are you facing some problem?

nope, paranoia just. I wan't to make sure the PangoLayout object destroyed for this method on Gtk side.

where can I implement g_object_unref method?

g_object_unrefwill not work for you. You are using pango https://docs.gtk.org/Pango/index.html?q=free

better not implement things that work with memory. just do it if you are facing problem

Well, I maybe write some tests later. Just interesting.

If right with that suppose, php-gtk would require additional objects management/sync implementation.

I have removed this object by the Label widget API

    protected static function _width(
        string $markup
    ): ?int
    {
        $label = new GtkLabel;

        $label->set_use_markup(
            true
        );

        $label->set_markup(
            $markup
        );

        if ($size = $label->get_layout()->get_pixel_size())
        {
            $label->destroy(); // remove widget with layout inside

            return $size['width'];
        }

        return null;
    }