Removed all calls to `Marshal.ReleaseComObject` where there exists a possibility that the interface could have been referenced by another object and changed to nulling the holding variable. See discussion thread #188.

pull/211/head
dahall 2021-02-01 16:08:43 -07:00
parent 4ad9cc77e2
commit 967389e246
31 changed files with 41 additions and 162 deletions

View File

@ -716,7 +716,6 @@ namespace Vanara.IO
}
catch { }
Files = null;
Marshal.FinalReleaseComObject(m_ijob);
m_ijob = null;
m_notifier = null;
}

View File

@ -14,7 +14,7 @@ namespace Vanara.InteropServices
}
/// <summary>
/// A safe variable to hold an instance of a COM class that automatically calls <see cref="Marshal.ReleaseComObject(object)"/> on disposal.
/// A safe variable to hold an instance of a COM class that automatically releases the instance on disposal.
/// </summary>
/// <typeparam name="T">The type of the COM object.</typeparam>
/// <seealso cref="System.IDisposable"/>
@ -59,8 +59,6 @@ namespace Vanara.InteropServices
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public void Dispose()
{
if (Item == null) return;
Marshal.ReleaseComObject(Item);
Item = null;
}
}

View File

@ -6856,7 +6856,7 @@ namespace Vanara.PInvoke
object IEnumerator.Current => Current;
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public void Dispose() => Marshal.ReleaseComObject(opcEnum);
public void Dispose() => opcEnum = default;
/// <summary>Advances the enumerator to the next element of the collection.</summary>
/// <returns>
@ -6874,7 +6874,6 @@ namespace Vanara.PInvoke
public void Reset()
{
var clone = opcEnum.InvokeMethod<TEnum>("Clone");
Marshal.ReleaseComObject(opcEnum);
opcEnum = clone;
}
}

View File

@ -409,7 +409,7 @@ namespace Vanara.PInvoke
private class ManualParentAndItem : IParentAndItem, IDisposable
{
private readonly PIDL pChild;
private readonly IShellFolder psf;
private IShellFolder psf;
public ManualParentAndItem(IShellItem psi)
{
@ -421,7 +421,7 @@ namespace Vanara.PInvoke
void IDisposable.Dispose()
{
Marshal.ReleaseComObject(psf);
psf = null;
pChild.Dispose();
}

View File

@ -58,13 +58,7 @@ namespace Vanara.Network
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
void IDisposable.Dispose()
{
if (conn == null) return;
if (cost != null)
{
Marshal.FinalReleaseComObject(cost);
cost = null;
}
Marshal.FinalReleaseComObject(conn);
cost = null;
conn = null;
}
}

View File

@ -166,8 +166,6 @@ namespace Vanara.Network
void IDisposable.Dispose()
{
if (conns == null) return;
Marshal.FinalReleaseComObject(conns);
conns = null;
}
}

View File

@ -93,8 +93,6 @@ namespace Vanara.Network
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
void IDisposable.Dispose()
{
if (inet == null) return;
Marshal.FinalReleaseComObject(inet);
inet = null;
}

View File

@ -116,8 +116,8 @@ namespace Vanara.PInvoke.Tests
}
finally
{
foreach (var i in pList)
Marshal.ReleaseComObject(i);
GC.Collect();
GC.WaitForPendingFinalizers();
}
}

View File

@ -148,31 +148,13 @@ namespace Vanara.Configuration
var type = Type.GetTypeFromProgID("Wscript.Shell");
if (type != null)
{
object script = null;
try
var script = Activator.CreateInstance(type);
foreach (var file in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Recent), "*.lnk").Where(s => exts.Contains(Path.GetExtension(s.Substring(0, s.Length - 4)).Trim('.').ToLower())))
{
script = Activator.CreateInstance(type);
foreach (var file in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Recent), "*.lnk").Where(s => exts.Contains(Path.GetExtension(s.Substring(0, s.Length - 4)).Trim('.').ToLower())))
{
object sc = null;
try
{
sc = script.InvokeMethod<object>("CreateShortcut", file);
var targetPath = sc.GetPropertyValue<string>("TargetPath");
if (targetPath != null)
recentFiles.Add(targetPath);
}
finally
{
if (sc != null)
Marshal.FinalReleaseComObject(sc);
}
}
}
finally
{
if (script != null)
Marshal.FinalReleaseComObject(script);
var sc = script.InvokeMethod<object>("CreateShortcut", file);
var targetPath = sc.GetPropertyValue<string>("TargetPath");
if (targetPath != null)
recentFiles.Add(targetPath);
}
}
}

View File

@ -1077,7 +1077,6 @@ namespace Vanara.Windows.Forms
explorerBrowserControl.Destroy();
// release com reference to it
Marshal.ReleaseComObject(explorerBrowserControl);
explorerBrowserControl = null;
}
@ -1554,7 +1553,6 @@ namespace Vanara.Windows.Forms
viewDispatch = psv.GetItemObject(SVGIO.SVGIO_BACKGROUND, IID_IDispatch);
if (ConnectToConnectionPoint(this, IID_DShellFolderViewEvents, true, viewDispatch, ref viewConnectionPointCookie).Failed)
{
Marshal.ReleaseComObject(viewDispatch);
viewDispatch = null;
}
}
@ -1563,7 +1561,6 @@ namespace Vanara.Windows.Forms
{
if (viewDispatch is null) return;
ConnectToConnectionPoint(null, IID_DShellFolderViewEvents, false, viewDispatch, ref viewConnectionPointCookie);
Marshal.ReleaseComObject(viewDispatch);
viewDispatch = null;
viewConnectionPointCookie = 0;
}

View File

@ -934,14 +934,9 @@ namespace Vanara.Windows.Forms
if (adviseCookie > 0)
pCtrl.TreeUnadvise(adviseCookie);
SetSite(null);
Marshal.ReleaseComObject(pCtrl);
pCtrl = null;
}
if (pCtrl2 != null)
{
Marshal.ReleaseComObject(pCtrl2);
pCtrl2 = null;
}
pCtrl2 = null;
base.OnHandleDestroyed(e);
}

View File

@ -242,7 +242,6 @@ namespace Vanara.Windows.Forms
return;
if (!closed)
Stop();
Marshal.FinalReleaseComObject(iDlg);
iDlg = null;
}
}

View File

@ -38,7 +38,6 @@ namespace Vanara.Extensions
var ac = new IAutoComplete2();
ac.Init(textBox.Handle, new ComEnumStringImpl(items), null, null);
ac.SetOptions(options);
Marshal.ReleaseComObject(ac);
}
/// <summary>Sets the tab stops in a multiline edit control. When text is copied to the control, any tab character in the text causes space to be generated up to the next tab stop.</summary>

View File

@ -15,7 +15,7 @@ namespace Vanara.Windows.Shell
[ComVisible(true)]
public class BindContext : IDisposable, IBindCtxV, IBindCtx
{
private readonly IBindCtxV iBindCtx;
private IBindCtxV iBindCtx;
/// <summary>Initializes a new instance of the <see cref="BindContext"/> class.</summary>
public BindContext() => CreateBindCtx(0, out iBindCtx).ThrowIfFailed();
@ -130,7 +130,7 @@ namespace Vanara.Windows.Shell
}
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public void Dispose() => Marshal.FinalReleaseComObject(iBindCtx);
public void Dispose() => iBindCtx = null;
/// <summary>
/// Retrieves a pointer to an interface that can be used to enumerate the keys of the bind context's string-keyed table of pointers.

View File

@ -64,7 +64,7 @@ namespace Vanara.Windows.Shell
private class SafeCP : IDisposable
{
internal readonly IOpenControlPanel icp;
internal IOpenControlPanel icp;
private bool disposedValue = false;
public SafeCP() => icp = new IOpenControlPanel();
@ -94,7 +94,7 @@ namespace Vanara.Windows.Shell
protected virtual void Dispose(bool disposing)
{
if (disposedValue) return;
Marshal.ReleaseComObject(icp);
icp = null;
disposedValue = true;
}
}

View File

@ -221,7 +221,6 @@ namespace Vanara.Windows.Shell
using var prot = ComReleaserFactory.Create(rot);
try { rot.Revoke(regId); } catch { }
regId = 0;
Marshal.FinalReleaseComObject(moniker);
moniker = null;
}
}

View File

@ -306,7 +306,6 @@ namespace Vanara.Windows.Shell
iProgressDialog.StopProgressDialog();
Thread.Sleep(500);
Marshal.FinalReleaseComObject(iProgressDialog);
iProgressDialog = null;
}

View File

@ -581,11 +581,7 @@ namespace Vanara.Windows.Shell
}
if (sink != null) op.Unadvise(sinkCookie);
if (op != null)
{
Marshal.FinalReleaseComObject(op);
op = null;
}
op = null;
disposedValue = true;
}
}

View File

@ -156,8 +156,6 @@ namespace Vanara.Windows.Shell
void IDisposable.Dispose()
{
if (changes == null) return;
Marshal.FinalReleaseComObject(changes);
changes = null;
}

View File

@ -29,7 +29,6 @@ namespace Vanara.Windows.Shell
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public virtual void Dispose()
{
Marshal.ReleaseComObject(iObj);
iObj = null;
}

View File

@ -313,7 +313,6 @@ namespace Vanara.Windows.Shell
}
m_MessageWindow?.Dispose();
Marshal.ReleaseComObject(ComInterface);
ComInterface = null;
disposedValue = true;
}

View File

@ -170,7 +170,7 @@ namespace Vanara.Windows.Shell
/// </summary>
public override void Dispose()
{
if (iShellFolder != null) { Marshal.ReleaseComObject(iShellFolder); iShellFolder = null; }
iShellFolder = null;
base.Dispose();
}

View File

@ -581,9 +581,9 @@ namespace Vanara.Windows.Shell
{
if (props != null) { props?.Dispose(); props = null; }
if (propDescList != null) { propDescList?.Dispose(); propDescList = null; }
if (qi != null) { Marshal.ReleaseComObject(qi); qi = null; }
if (iShellItem2 != null) { Marshal.ReleaseComObject(iShellItem2); iShellItem2 = null; }
if (iShellItem != null) { Marshal.ReleaseComObject(iShellItem); iShellItem = null; }
qi = null;
iShellItem2 = null;
iShellItem = null;
}
/// <summary>Determines whether the specified <see cref="System.Object"/>, is equal to this instance.</summary>

View File

@ -100,11 +100,7 @@ namespace Vanara.Windows.Shell
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public virtual void Dispose()
{
if (array != null)
{
Marshal.ReleaseComObject(array);
array = null;
}
array = null;
}
/// <summary>Returns an enumerator that iterates through the collection.</summary>

View File

@ -150,11 +150,7 @@ namespace Vanara.Windows.Shell
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public override void Dispose()
{
if (lib != null)
{
Marshal.ReleaseComObject(lib);
lib = null;
}
lib = null;
base.Dispose();
}
@ -221,11 +217,7 @@ namespace Vanara.Windows.Shell
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public override void Dispose()
{
if (lib != null)
{
Marshal.ReleaseComObject(lib);
lib = null;
}
lib = null;
base.Dispose();
}

View File

@ -290,7 +290,7 @@ namespace Vanara.Windows.Shell
/// <summary>Dispose the object, releasing the COM ShellLink object</summary>
public override void Dispose()
{
if (link != null) { Marshal.ReleaseComObject(link); link = null; }
link = null;
//Release(link);
base.Dispose();
}

View File

@ -170,11 +170,7 @@ namespace Vanara.Windows.Shell
/// </summary>
public void Dispose()
{
if (condition != null)
{
Marshal.ReleaseComObject(condition);
condition = null;
}
condition = null;
}
/// <summary>Gets the results of the condition by level.</summary>

View File

@ -335,26 +335,9 @@ namespace Vanara.Windows.Shell
User32.DestroyWindow(shellViewWindow);
shellViewWindow = HWND.NULL;
}
if (iBrowser != null)
{
if (iBrowser.GetType().IsCOMObject)
Marshal.ReleaseComObject(iBrowser);
iBrowser = null;
}
//if (Items != null)
//{
// Items.Dispose();
// Items = null;
//}
if (History != null)
{
History = null;
}
if (IShellView != null)
{
Marshal.ReleaseComObject(IShellView);
IShellView = null;
}
iBrowser = null;
History = null;
IShellView = null;
}
base.Dispose(disposing);
}
@ -437,7 +420,7 @@ namespace Vanara.Windows.Shell
{
prev.UIActivate(SVUIA.SVUIA_DEACTIVATE);
prev.DestroyViewWindow();
Marshal.ReleaseComObject(prev);
prev = null;
}
IShellView.UIActivate(SVUIA.SVUIA_ACTIVATE_NOFOCUS);

View File

@ -133,16 +133,8 @@ namespace Vanara.Windows.Shell
public virtual void Dispose()
{
// typeList?.Dispose();
if (iDesc2 != null)
{
Marshal.ReleaseComObject(iDesc2);
iDesc2 = null;
}
if (iDesc != null)
{
Marshal.ReleaseComObject(iDesc);
iDesc = null;
}
iDesc2 = null;
iDesc = null;
}
/// <summary>Gets a formatted string representation of a property value.</summary>
@ -233,11 +225,7 @@ namespace Vanara.Windows.Shell
/// <inheritdoc />
public virtual void Dispose()
{
if (iList != null)
{
Marshal.ReleaseComObject(iList);
iList = null;
}
iList = null;
}
/// <inheritdoc />
@ -354,16 +342,8 @@ namespace Vanara.Windows.Shell
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public virtual void Dispose()
{
if (iType2 != null)
{
Marshal.ReleaseComObject(iType2);
iType2 = null;
}
if (iType != null)
{
Marshal.ReleaseComObject(iType);
iType = null;
}
iType2 = null;
iType = null;
}
/// <summary>Returns a <see cref="System.String" /> that represents this instance.</summary>
@ -399,11 +379,7 @@ namespace Vanara.Windows.Shell
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public virtual void Dispose()
{
if (iList != null)
{
Marshal.ReleaseComObject(iList);
iList = null;
}
iList = null;
}
/// <summary>Determines the index of a specific item in the list.</summary>

View File

@ -92,11 +92,7 @@ namespace Vanara.Windows.Shell
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public virtual void Dispose()
{
if (iPropertyStore != null)
{
Marshal.ReleaseComObject(iPropertyStore);
iPropertyStore = null;
}
iPropertyStore = null;
}
/// <summary>Gets the property.</summary>

View File

@ -503,16 +503,8 @@ namespace Vanara.Windows.Shell
{
~TaskbarListStaticFinalizer()
{
if (taskbar2 != null)
{
Marshal.ReleaseComObject(taskbar2);
taskbar2 = null;
}
if (taskbar4 != null)
{
Marshal.ReleaseComObject(taskbar4);
taskbar4 = null;
}
taskbar2 = null;
taskbar4 = null;
}
}
}