Completely missing gradients, clips, filters after undo
Summary: sometimes gradients used in a document seem to be lost after restarting the program.
#### Steps to reproduce:
Hopefully this is the only way to trigger, but below steps were identified by @miga as one way to trigger:
- open Inkscape
- open the Export dialog and F&S dialog
- open [bug_demo.svg](https://gitlab.com/inkscape/inkscape/uploads/d52f3c70e07c0f33c38b17c0195ba1d1/bug_demo.svg), alternatively draw 2 rectangles and give them different gradients
- copy all the objects in the Instagram page, or if starting from scratch, copy both rectangles
- paste
- undo
- save and reopen (or just open the XML editor and see that the defs tag is empty)
#### What happened?
As far as I can understand/inspect, gradients are still defined in the XML [Ex: "fill: url(#linearGradient29315)"] but they are not available to be seen or set to as fill. It's like they gone missing from whatever place they are located in the SVG. I can't always replicate this bug since I was not able to detect any pattern by why exactly this happens, I'm only submitting after noticing the same issue with over 5+ files. The only common thing between then is the use of pages.
Also crash if in a debug build.
#### What should have happened?
Display the gradients properly as usual
Sample attachments:


There's a text in the second page, it's possible to be selected and will conform to any new fill, even gradients, but the original gradient referred to it is missing.
#### Version info
Bisection points to eac403ed95e92ab805b19eb1e872dc946016e9e1 or 492fc651382002f388a36ddb37c3226afb4d22ab https://gitlab.com/inkscape/inkscape/-/commit/492fc651382002f388a36ddb37c3226afb4d22ab , though since this involves multiple dialogs, and multiple pages, these commits may not be the only relevant changes.
```
Inkscape 1.2 (dc2aedaf03, 2022-05-15)
GLib version: 2.72.1
GTK version: 3.24.34
glibmm version: 2.66.1
gtkmm version: 3.24.5
libxml2 version: 2.9.12
libxslt version: 1.1.34
Cairo version: 1.16.0
Pango version: 1.50.7
HarfBuzz version: 3.0.0
OS version: GNOME 42 (Flatpak runtime)
```
issue