Critical getting a strong reference from a weak ref after its value has been destroyed
I get a GLib-GObject-CRITICAL assigning a weak var to a nullable strong ref when the object the weak value points to as been destroyed:
GLib.Object strong_ref = new GLib.Object();
weak GLib.Object weak_ref = strong_ref1;
strong_ref = null;
// GLib-GObject-CRITICAL here
GLib.Object? nullable_strong = weak_ref;
My use case is in Geary, using a Gee Map with weak GObject derived values as a cache: If the map does not contain a key, or if the object the weak value points to as been destroyed, I create a new instance and return that. I need a strong reference to the cached value, since I can't assign the new instance to a weak var, but if the object refered to by the weak var has been destroyed, I get a GLib-GObject-CRITICAL, even if the strong ref is nullable:
Gee.Map<string,weak Endpoint> shared_endpoints =
new Gee.HashMap<string,weak Endpoint>();
...
// critical here if the entry has been destroyed
Endpoint? shared = shared_endpoints.get(key);
if (shared == null) {
shared == ...;
shared_endpoints.set(key, shared);
}
return shared;
It's not related to the map, that's just the use case.
Minimal test case, compile with valac--pkg gtk+-3.0 weak-ref-crash.vala
: weak-ref-crash.vala