On Mon May 19 10:35:52 2025 +0000, Zhiyi Zhang wrote:
The attached tests show that releasing the weak ref interface releases the main object as well. So the weak ref interface resides in the main object. As for releasing the main object while the weak ref is still alive. I guess you can release most of the allocated memory in uisettings_Release() when ref_strong is 0? It's just the wrapper object, for example `struct uisettings`, for the interfaces and refcount is still active. [weakref-tests.diff](/uploads/805f58bc9206095968936f38039f9605/weakref-tests.diff)
``` + /* Test that weakref interface resides in the same object */ + hr = RoActivateInstance( str, &inspectable ); + ok( hr == S_OK, "Got unexpected hr %#lx.\n", hr ); + ref = get_refcount(inspectable); + ok( ref == 1, "got ref %ld.\n", ref ); + + hr = IInspectable_QueryInterface( inspectable, &IID_IWeakReferenceSource, (void **)&weak_reference_source ); + ok( hr == S_OK, "got hr %#lx.\n", hr ); + hr = IWeakReferenceSource_GetWeakReference( weak_reference_source, &weak_reference ); + ok( hr == S_OK, "got hr %#lx.\n", hr ); + IWeakReferenceSource_Release( weak_reference_source ); + ref = get_refcount(weak_reference); + ok( ref == 2, "got ref %ld.\n", ref ); + ref = IWeakReference_Release( weak_reference ); + ok( ref == 1, "got ref %ld.\n", ref ); + ref = get_refcount(inspectable); + ok( ref == 1, "got ref %ld.\n", ref ); + + ref = IWeakReference_Release( weak_reference ); + ok( ref == 0, "got ref %ld.\n", ref ); ```
You can't do second release on something you don't own. You can only release as many times as you called GetWeakReference().
``` + ref = get_refcount(inspectable); + /* Now ref for the main object is non-sense, meaning releasing weakref ends up releasing the main object as well */ + ok( ref == 1, "got ref %ld.\n", ref ); + ```