Skip to content

Commit df78b5b

Browse files
authored
Merge pull request #12289 from lonitra/backport12281
[release/8.0] Ensure proper ref count of underlying ocx
2 parents 34d50ce + 0295379 commit df78b5b

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/System.Windows.Forms/src/System/Windows/Forms/AxHost.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2326,13 +2326,13 @@ private void SetOcState(int nv)
23262326
private void CreateWithoutLicense(Guid clsid)
23272327
{
23282328
s_axHTraceSwitch.TraceVerbose($"Creating object without license: {clsid}");
2329-
IUnknown* unknown;
2329+
using ComScope<IUnknown> unknown = new(null);
23302330
HRESULT hr = PInvoke.CoCreateInstance(
23312331
&clsid,
23322332
(IUnknown*)null,
23332333
CLSCTX.CLSCTX_INPROC_SERVER,
23342334
IID.Get<IUnknown>(),
2335-
(void**)&unknown);
2335+
unknown);
23362336
hr.ThrowOnFailure();
23372337

23382338
_instance = Marshal.GetObjectForIUnknown((nint)unknown);
@@ -2356,8 +2356,8 @@ private void CreateWithLicense(string? license, Guid clsid)
23562356

23572357
if (hr.Succeeded)
23582358
{
2359-
IUnknown* unknown;
2360-
hr = factory.Value->CreateInstanceLic(null, null, IID.Get<IUnknown>(), new BSTR(license), (void**)&unknown);
2359+
using ComScope<IUnknown> unknown = new(null);
2360+
hr = factory.Value->CreateInstanceLic(null, null, IID.Get<IUnknown>(), new BSTR(license), unknown);
23612361
hr.ThrowOnFailure();
23622362

23632363
_instance = Marshal.GetObjectForIUnknown((nint)unknown);
@@ -3176,14 +3176,14 @@ public unsafe void ShowPropertyPages(Control control)
31763176
transaction = host?.CreateTransaction(SR.AXEditProperties);
31773177

31783178
HWND handle = ContainingControl is null ? HWND.Null : ContainingControl.HWND;
3179-
IUnknown* unknown = ComHelpers.GetComPointer<IUnknown>(_instance);
3179+
using var unknown = ComHelpers.GetComScope<IUnknown>(_instance);
31803180
hr = PInvoke.OleCreatePropertyFrame(
31813181
handle,
31823182
0,
31833183
0,
31843184
(PCWSTR)null,
31853185
1,
3186-
&unknown,
3186+
unknown,
31873187
uuids.cElems,
31883188
uuids.pElems,
31893189
PInvoke.GetThreadLocale(),

src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/AxHostTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3073,6 +3073,18 @@ public void AxHost_ICustomTypeDescriptorGetPropertyOwner_InvokeWithHandle_Return
30733073
Assert.Equal(0, createdCallCount);
30743074
}
30753075

3076+
[WinFormsFact]
3077+
public unsafe void AxHost_Ocx_Dispose_Success()
3078+
{
3079+
SubAxHost control = new(WebBrowserClsidString);
3080+
control.CreateControl();
3081+
using var ocx = ComHelpers.GetComScope<IUnknown>(control.GetOcx());
3082+
3083+
control.Dispose();
3084+
ocx.Value->AddRef();
3085+
Assert.Equal((uint)0, ocx.Value->Release() - 1);
3086+
}
3087+
30763088
private class SubComponentEditor : ComponentEditor
30773089
{
30783090
public override bool EditComponent(ITypeDescriptorContext context, object component)

0 commit comments

Comments
 (0)