Segmentation Fault and weird errors

Code that causes the problem:

    def delete_pages(self, *_) -> None:
        self.main_stk.set_visible_child_name("main_page")
        # remove all pages from stack1
        for page in self.pages:
            page_id = self.get_page_id(page)
            page_child = self.stack1.get_child_by_name(page_id)
            self.stack1.remove(page_child)

can someone suggest a better way to remove all pages from a stack

If self.pages is a list model of Gtk.StackPage instances in stack1 then it’s not safe to remove items while iterating over a list.

You have to collect all pages into a separate list, and then remove them:

def delete_pages(self):
    pages = [self.get_page_id(x) for x in self.pages]
    for page_id in pages:
        w = self.stack1.get_child_by_name(page_id)
        self.stack1.remove(w)

in the console I still get is it normal or should I ignore it

/Users/panda/sshfs/bakery-gui.py:1599: Warning: instance with invalid (NULL) class pointer
  self.stack1.remove(w)
/Users/panda/sshfs/bakery-gui.py:1599: Warning: g_signal_handlers_disconnect_matched: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed
  self.stack1.remove(w)
/Users/panda/sshfs/bakery-gui.py:1599: Warning: instance of invalid non-instantiatable type '(null)'
  self.stack1.remove(w)

That’s coming from GTK itself; which version of GTK are you using?

image

Could you try this example, and see if it emits the same warning:

import gi

gi.require_version("Gtk", "4.0")
from gi.repository import GLib, Gtk

stack = Gtk.Stack()
stack.add_named(Gtk.Button(label="Hello"), "foo")
stack.add_named(Gtk.Button(label="World"), "bar")
stack.add_named(Gtk.Button(label="Goodbye"), "baz")

pages = stack.get_pages()
assert len(pages) == 3

names = [x.get_name() for x in pages]
assert len(names) == len(pages)

print(names)

for name in names:
    w = stack.get_child_by_name(name)
    stack.remove(w)

assert len(pages) == 0

Do you use a Gtk.StackSideBar with the Stack?

If yes, then I’m pretty sure the crash is related to this issue.

is there any fix it still happens

You haven’t answered the question from me or @gwillems, so “is there any fix” isn’t really something we can answer.

yes I do its called stack1_sidebar in my code

OK, so I’m quite sure it’s the same issue I reported.

There is a fix under review, may take a little bit of time till it gets reviewed/integrated/released…

In the meantime, you could tweak your code to keep a reference to the page, to avoid the use-after-free crash.

Maybe something like this should be enough:

        for page in self.pages:
            page_id = self.get_page_id(page)
            page_child = self.stack1.get_child_by_name(page_id)
            self.stack1.remove(page_child)
            page   # does nothing, just here to keep an active reference during remove()

Thanks will try this when I can
Is there a issue/merge I can track

It still seg faults even when calling page

hmm, now I’m confused…

What about this?

        for page in list(self.pages):
            page_child = page.get_child()
            self.stack1.remove(page_child)
Traceback (most recent call last):
  File "/home/panda/Bakery/bakery-gui.py", line 278, in delete_pages
    page_child = page.get_child()
                 ^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'get_child'

here’s the code if u wanna take a deeper look

Ah OK, thanks, I see, I thought self.pages was a list model of Gtk.StackPages

Can you please try this?

        for page in self.pages:
            page_id = self.get_page_id(page)
            page_child = self.stack1.get_child_by_name(page_id)
            stackpage = self.stack1.get_page(page_child)
            self.stack1.remove(page_child)
            stackpage   # does nothing, just used to keep an active reference during remove()