scorninpc / php-gtk3

GTK3 extension for PHP
https://andor.com.br/
GNU Lesser General Public License v3.0
114 stars 11 forks source link

g_object_unref or how to destroy objects created? #151

Closed d47081 closed 2 months ago

d47081 commented 2 months ago

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?

scorninpc commented 2 months ago

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

d47081 commented 2 months ago

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.

scorninpc commented 2 months ago

are you facing some problem?

d47081 commented 2 months ago

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?

scorninpc commented 2 months ago

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

d47081 commented 2 months ago

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

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

d47081 commented 2 months ago

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;
    }