From: Zhiyi Zhang zzhang@codeweavers.com
This also fix a double free in uisettings_Release(). --- dlls/windows.ui/Makefile.in | 3 +- dlls/windows.ui/uisettings.c | 139 ++++------------------------------- dlls/windows.ui/weakref.c | 114 ++++++++++++++++++++++++++++ dlls/windows.ui/weakref.h | 81 ++++++++++++++++++++ 4 files changed, 211 insertions(+), 126 deletions(-) create mode 100644 dlls/windows.ui/weakref.c create mode 100644 dlls/windows.ui/weakref.h
diff --git a/dlls/windows.ui/Makefile.in b/dlls/windows.ui/Makefile.in index 953991cafba..e88a40188b1 100644 --- a/dlls/windows.ui/Makefile.in +++ b/dlls/windows.ui/Makefile.in @@ -6,4 +6,5 @@ SOURCES = \ inputpane.c \ main.c \ uisettings.c \ - uiviewsettings.c + uiviewsettings.c \ + weakref.c diff --git a/dlls/windows.ui/uisettings.c b/dlls/windows.ui/uisettings.c index d5eae44b797..e5032389da7 100644 --- a/dlls/windows.ui/uisettings.c +++ b/dlls/windows.ui/uisettings.c @@ -19,7 +19,7 @@
#include "private.h" #include "initguid.h" -#include "weakreference.h" +#include "weakref.h"
#include "wine/debug.h"
@@ -35,9 +35,7 @@ struct uisettings IUISettings4 IUISettings4_iface; IUISettings5 IUISettings5_iface; IWeakReferenceSource IWeakReferenceSource_iface; - IWeakReference IWeakReference_iface; - LONG ref_strong; - LONG ref_weak; + IWeakReference *weakref; };
static inline struct uisettings *impl_from_IUISettings( IUISettings *iface ) @@ -94,20 +92,18 @@ static HRESULT WINAPI uisettings_QueryInterface( IUISettings *iface, REFIID iid, static ULONG WINAPI uisettings_AddRef( IUISettings *iface ) { struct uisettings *impl = impl_from_IUISettings( iface ); - ULONG ref = InterlockedIncrement( &impl->ref_strong ); + ULONG ref = weak_reference_strong_add_ref( impl->weakref ); TRACE( "iface %p, ref %lu.\n", iface, ref ); - IWeakReference_AddRef( &impl->IWeakReference_iface ); return ref; }
static ULONG WINAPI uisettings_Release( IUISettings *iface ) { struct uisettings *impl = impl_from_IUISettings( iface ); - ULONG ref = InterlockedDecrement( &impl->ref_strong ); + ULONG ref = weak_reference_strong_release( impl->weakref );
TRACE( "iface %p, ref %lu.\n", iface, ref );
- IWeakReference_Release( &impl->IWeakReference_iface ); if (!ref) free( impl ); return ref; } @@ -480,119 +476,7 @@ static const struct IUISettings5Vtbl uisettings5_vtbl = uisettings5_remove_AutoHideScrollBarsChanged, };
-static inline struct uisettings *impl_from_IWeakReference( IWeakReference *iface ) -{ - return CONTAINING_RECORD( iface, struct uisettings, IWeakReference_iface ); -} - -static HRESULT WINAPI weak_reference_QueryInterface( IWeakReference *iface, REFIID iid, void **out ) -{ - struct uisettings *impl = impl_from_IWeakReference( iface ); - - TRACE( "iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out ); - - if (IsEqualGUID( iid, &IID_IUnknown ) || - IsEqualGUID( iid, &IID_IWeakReference )) - { - *out = &impl->IWeakReference_iface; - IInspectable_AddRef( *out ); - return S_OK; - } - - FIXME( "%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid) ); - *out = NULL; - return E_NOINTERFACE; -} - -static ULONG WINAPI weak_reference_AddRef( IWeakReference *iface ) -{ - struct uisettings *impl = impl_from_IWeakReference( iface ); - ULONG ref = InterlockedIncrement( &impl->ref_weak ); - TRACE("iface %p increasing refcount to %lu.\n", iface, ref); - return ref; -} - -static ULONG WINAPI weak_reference_Release( IWeakReference *iface ) -{ - struct uisettings *impl = impl_from_IWeakReference( iface ); - ULONG ref = InterlockedDecrement( &impl->ref_weak ); - if (!ref) - free( impl ); - return ref; -} - -static HRESULT WINAPI weak_reference_Resolve( IWeakReference *iface, REFIID iid, IInspectable **out ) -{ - struct uisettings *impl = impl_from_IWeakReference( iface ); - HRESULT hr; - LONG ref; - - TRACE( "iface %p, iid %s, out %p stub.\n", iface, debugstr_guid(iid), out ); - - *out = NULL; - - do - { - if (!(ref = ReadNoFence( &impl->ref_strong ))) - return S_OK; - } while (ref != InterlockedCompareExchange( &impl->ref_strong, ref + 1, ref )); - - hr = IUISettings_QueryInterface( &impl->IUISettings_iface, iid, (void **)out ); - InterlockedDecrement( &impl->ref_strong ); - return hr; -} - -static const struct IWeakReferenceVtbl weak_reference_vtbl = -{ - weak_reference_QueryInterface, - weak_reference_AddRef, - weak_reference_Release, - /* IWeakReference methods */ - weak_reference_Resolve, -}; - -static inline struct uisettings *impl_from_IWeakReferenceSource( IWeakReferenceSource *iface ) -{ - return CONTAINING_RECORD( iface, struct uisettings, IWeakReferenceSource_iface ); -} - -static HRESULT WINAPI weak_reference_source_QueryInterface( IWeakReferenceSource *iface, REFIID iid, void **out ) -{ - struct uisettings *impl = impl_from_IWeakReferenceSource( iface ); - return uisettings_QueryInterface( &impl->IUISettings_iface, iid, out ); -} - -static ULONG WINAPI weak_reference_source_AddRef( IWeakReferenceSource *iface ) -{ - struct uisettings *impl = impl_from_IWeakReferenceSource( iface ); - return uisettings_AddRef( &impl->IUISettings_iface ); -} - -static ULONG WINAPI weak_reference_source_Release( IWeakReferenceSource *iface ) -{ - struct uisettings *impl = impl_from_IWeakReferenceSource( iface ); - return uisettings_Release( &impl->IUISettings_iface ); -} - -static HRESULT WINAPI weak_reference_source_GetWeakReference( IWeakReferenceSource *iface, IWeakReference **ref ) -{ - struct uisettings *impl = impl_from_IWeakReferenceSource(iface); - - TRACE("iface %p, ref %p stub.\n", iface, ref); - - *ref = &impl->IWeakReference_iface; - IWeakReference_AddRef( *ref ); - return S_OK; -} - -static const struct IWeakReferenceSourceVtbl weak_reference_source_vtbl = -{ - weak_reference_source_QueryInterface, - weak_reference_source_AddRef, - weak_reference_source_Release, - /* IWeakReferenceSource methods */ - weak_reference_source_GetWeakReference, -}; +DEFINE_WEAKREF_SOURCE(uisettings, IUISettings, struct uisettings);
struct uisettings_statics { @@ -662,6 +546,7 @@ static HRESULT WINAPI factory_GetTrustLevel( IActivationFactory *iface, TrustLev static HRESULT WINAPI factory_ActivateInstance( IActivationFactory *iface, IInspectable **instance ) { struct uisettings *impl; + HRESULT hr;
TRACE( "iface %p, instance %p.\n", iface, instance );
@@ -676,10 +561,14 @@ static HRESULT WINAPI factory_ActivateInstance( IActivationFactory *iface, IInsp impl->IUISettings3_iface.lpVtbl = &uisettings3_vtbl; impl->IUISettings4_iface.lpVtbl = &uisettings4_vtbl; impl->IUISettings5_iface.lpVtbl = &uisettings5_vtbl; - impl->IWeakReferenceSource_iface.lpVtbl = &weak_reference_source_vtbl; - impl->IWeakReference_iface.lpVtbl = &weak_reference_vtbl; - impl->ref_strong = 1; - impl->ref_weak = 1; + + impl->IWeakReferenceSource_iface.lpVtbl = &uisettings_weakref_source_vtbl; + if (FAILED(hr = weak_reference_create( (IUnknown *)&impl->IUISettings_iface, &impl->weakref ))) + { + *instance = NULL; + free( impl ); + return hr; + }
*instance = (IInspectable *)&impl->IUISettings5_iface; return S_OK; diff --git a/dlls/windows.ui/weakref.c b/dlls/windows.ui/weakref.c new file mode 100644 index 00000000000..f04e2f6ad7e --- /dev/null +++ b/dlls/windows.ui/weakref.c @@ -0,0 +1,114 @@ +/* WinRT weak reference helpers + * + * Copyright 2025 Zhiyi Zhang for CodeWeavers + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#define COBJMACROS +#include "weakref.h" + +static inline struct weakref *impl_from_IWeakReference( IWeakReference *iface ) +{ + return CONTAINING_RECORD(iface, struct weakref, IWeakReference_iface); +} + +static HRESULT WINAPI weakref_QueryInterface( IWeakReference *iface, REFIID iid, void **out ) +{ + struct weakref *impl = impl_from_IWeakReference( iface ); + + if (IsEqualGUID( iid, &IID_IUnknown ) || IsEqualGUID( iid, &IID_IWeakReference )) + { + *out = &impl->IWeakReference_iface; + IWeakReference_AddRef( *out ); + return S_OK; + } + + *out = NULL; + return E_NOINTERFACE; +} + +static ULONG WINAPI weakref_AddRef( IWeakReference *iface ) +{ + struct weakref *impl = impl_from_IWeakReference( iface ); + return InterlockedIncrement( &impl->ref_weak ); +} + +static ULONG WINAPI weakref_Release( IWeakReference *iface ) +{ + struct weakref *impl = impl_from_IWeakReference( iface ); + ULONG ref = InterlockedDecrement( &impl->ref_weak ); + + if (!ref) + free( impl ); + return ref; +} + +static HRESULT WINAPI weakref_Resolve( IWeakReference *iface, REFIID iid, IInspectable **out ) +{ + struct weakref *impl = impl_from_IWeakReference( iface ); + HRESULT hr; + LONG ref; + + *out = NULL; + do + { + if (!(ref = ReadNoFence( &impl->ref_strong ))) + return S_OK; + } while (ref != InterlockedCompareExchange( &impl->ref_strong, ref + 1, ref )); + + hr = IUnknown_QueryInterface( impl->object, iid, (void **)out ); + IUnknown_Release( impl->object ); + return hr; +} + +static const struct IWeakReferenceVtbl weakref_vtbl = +{ + weakref_QueryInterface, + weakref_AddRef, + weakref_Release, + weakref_Resolve, +}; + +HRESULT weak_reference_create( IUnknown *object, IWeakReference **out ) +{ + struct weakref *weakref; + + if (!(weakref = calloc( 1, sizeof(*weakref) ))) + return E_OUTOFMEMORY; + + weakref->IWeakReference_iface.lpVtbl = &weakref_vtbl; + weakref->ref_strong = 1; + weakref->ref_weak = 1; + weakref->object = object; + *out = &weakref->IWeakReference_iface; + return S_OK; +} + +ULONG weak_reference_strong_add_ref( IWeakReference *iface ) +{ + struct weakref *impl = impl_from_IWeakReference( iface ); + return InterlockedIncrement( &impl->ref_strong ); +} + +ULONG weak_reference_strong_release( IWeakReference *iface ) +{ + struct weakref *impl = impl_from_IWeakReference( iface ); + ULONG ref = InterlockedDecrement( &impl->ref_strong ); + + if (!ref) + IWeakReference_Release( &impl->IWeakReference_iface ); + return ref; +} diff --git a/dlls/windows.ui/weakref.h b/dlls/windows.ui/weakref.h new file mode 100644 index 00000000000..81d041f406f --- /dev/null +++ b/dlls/windows.ui/weakref.h @@ -0,0 +1,81 @@ +/* WinRT weak reference helpers + * + * Copyright 2025 Zhiyi Zhang for CodeWeavers + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#ifndef __WINE_WEAKREF_H +#define __WINE_WEAKREF_H + +#include "weakreference.h" + +/* The control block is contained in struct weakref so that we don't have to allocate a separate + * struct for it */ +struct weakref +{ + IWeakReference IWeakReference_iface; + IUnknown *object; + /* control block */ + LONG ref_strong; + LONG ref_weak; +}; + +/* Create a IWeakReference managing the object */ +HRESULT weak_reference_create( IUnknown *object, IWeakReference **out ); +/* Add a strong reference to the managed object */ +ULONG weak_reference_strong_add_ref( IWeakReference *iface ); +/* Release a strong reference to the managed object */ +ULONG weak_reference_strong_release( IWeakReference *iface ); + +#define DEFINE_WEAKREF_SOURCE(pfx, iface_type, impl_type) \ +static inline impl_type *pfx##_impl_from_IWeakReferenceSource( IWeakReferenceSource *iface ) \ +{ \ + return CONTAINING_RECORD(iface, impl_type, IWeakReferenceSource_iface); \ +} \ +static HRESULT WINAPI pfx##_weakref_source_QueryInterface( IWeakReferenceSource *iface, \ + REFIID iid, void **out ) \ +{ \ + impl_type *impl = pfx##_impl_from_IWeakReferenceSource( iface ); \ + return pfx##_QueryInterface( &impl->iface_type##_iface, iid, out ); \ +} \ +static ULONG WINAPI pfx##_weakref_source_AddRef( IWeakReferenceSource *iface ) \ +{ \ + impl_type *impl = pfx##_impl_from_IWeakReferenceSource( iface ); \ + return pfx##_AddRef( &impl->iface_type##_iface ); \ +} \ +static ULONG WINAPI pfx##_weakref_source_Release( IWeakReferenceSource *iface ) \ +{ \ + impl_type *impl = pfx##_impl_from_IWeakReferenceSource( iface ); \ + return pfx##_Release( &impl->iface_type##_iface ); \ +} \ +static HRESULT WINAPI pfx##_weakref_source_GetWeakReference( IWeakReferenceSource *iface, \ + IWeakReference **ref ) \ +{ \ + impl_type *impl = pfx##_impl_from_IWeakReferenceSource( iface ); \ + TRACE("iface %p, ref %p.\n", iface, ref); \ + *ref = impl->weakref; \ + IWeakReference_AddRef( *ref ); \ + return S_OK; \ +} \ +static const struct IWeakReferenceSourceVtbl pfx##_weakref_source_vtbl = \ +{ \ + pfx##_weakref_source_QueryInterface, \ + pfx##_weakref_source_AddRef, \ + pfx##_weakref_source_Release, \ + pfx##_weakref_source_GetWeakReference, \ +}; + +#endif /* __WINE_WEAKREF_H */