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 { } catch { }
Files = null; Files = null;
Marshal.FinalReleaseComObject(m_ijob);
m_ijob = null; m_ijob = null;
m_notifier = null; m_notifier = null;
} }

View File

@ -14,7 +14,7 @@ namespace Vanara.InteropServices
} }
/// <summary> /// <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> /// </summary>
/// <typeparam name="T">The type of the COM object.</typeparam> /// <typeparam name="T">The type of the COM object.</typeparam>
/// <seealso cref="System.IDisposable"/> /// <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> /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public void Dispose() public void Dispose()
{ {
if (Item == null) return;
Marshal.ReleaseComObject(Item);
Item = null; Item = null;
} }
} }

View File

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

View File

@ -409,7 +409,7 @@ namespace Vanara.PInvoke
private class ManualParentAndItem : IParentAndItem, IDisposable private class ManualParentAndItem : IParentAndItem, IDisposable
{ {
private readonly PIDL pChild; private readonly PIDL pChild;
private readonly IShellFolder psf; private IShellFolder psf;
public ManualParentAndItem(IShellItem psi) public ManualParentAndItem(IShellItem psi)
{ {
@ -421,7 +421,7 @@ namespace Vanara.PInvoke
void IDisposable.Dispose() void IDisposable.Dispose()
{ {
Marshal.ReleaseComObject(psf); psf = null;
pChild.Dispose(); 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> /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
void IDisposable.Dispose() void IDisposable.Dispose()
{ {
if (conn == null) return; cost = null;
if (cost != null)
{
Marshal.FinalReleaseComObject(cost);
cost = null;
}
Marshal.FinalReleaseComObject(conn);
conn = null; conn = null;
} }
} }

View File

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

View File

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

View File

@ -148,31 +148,13 @@ namespace Vanara.Configuration
var type = Type.GetTypeFromProgID("Wscript.Shell"); var type = Type.GetTypeFromProgID("Wscript.Shell");
if (type != null) if (type != null)
{ {
object script = null; var script = Activator.CreateInstance(type);
try 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); var sc = script.InvokeMethod<object>("CreateShortcut", file);
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()))) var targetPath = sc.GetPropertyValue<string>("TargetPath");
{ if (targetPath != null)
object sc = null; recentFiles.Add(targetPath);
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);
} }
} }
} }

View File

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

View File

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

View File

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

View File

@ -38,7 +38,6 @@ namespace Vanara.Extensions
var ac = new IAutoComplete2(); var ac = new IAutoComplete2();
ac.Init(textBox.Handle, new ComEnumStringImpl(items), null, null); ac.Init(textBox.Handle, new ComEnumStringImpl(items), null, null);
ac.SetOptions(options); 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> /// <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)] [ComVisible(true)]
public class BindContext : IDisposable, IBindCtxV, IBindCtx 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> /// <summary>Initializes a new instance of the <see cref="BindContext"/> class.</summary>
public BindContext() => CreateBindCtx(0, out iBindCtx).ThrowIfFailed(); 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> /// <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> /// <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. /// 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 private class SafeCP : IDisposable
{ {
internal readonly IOpenControlPanel icp; internal IOpenControlPanel icp;
private bool disposedValue = false; private bool disposedValue = false;
public SafeCP() => icp = new IOpenControlPanel(); public SafeCP() => icp = new IOpenControlPanel();
@ -94,7 +94,7 @@ namespace Vanara.Windows.Shell
protected virtual void Dispose(bool disposing) protected virtual void Dispose(bool disposing)
{ {
if (disposedValue) return; if (disposedValue) return;
Marshal.ReleaseComObject(icp); icp = null;
disposedValue = true; disposedValue = true;
} }
} }

View File

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

View File

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

View File

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

View File

@ -156,8 +156,6 @@ namespace Vanara.Windows.Shell
void IDisposable.Dispose() void IDisposable.Dispose()
{ {
if (changes == null) return;
Marshal.FinalReleaseComObject(changes);
changes = null; 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> /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public virtual void Dispose() public virtual void Dispose()
{ {
Marshal.ReleaseComObject(iObj);
iObj = null; iObj = null;
} }

View File

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

View File

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

View File

@ -581,9 +581,9 @@ namespace Vanara.Windows.Shell
{ {
if (props != null) { props?.Dispose(); props = null; } if (props != null) { props?.Dispose(); props = null; }
if (propDescList != null) { propDescList?.Dispose(); propDescList = null; } if (propDescList != null) { propDescList?.Dispose(); propDescList = null; }
if (qi != null) { Marshal.ReleaseComObject(qi); qi = null; } qi = null;
if (iShellItem2 != null) { Marshal.ReleaseComObject(iShellItem2); iShellItem2 = null; } iShellItem2 = null;
if (iShellItem != null) { Marshal.ReleaseComObject(iShellItem); iShellItem = null; } iShellItem = null;
} }
/// <summary>Determines whether the specified <see cref="System.Object"/>, is equal to this instance.</summary> /// <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> /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public virtual void Dispose() public virtual void Dispose()
{ {
if (array != null) array = null;
{
Marshal.ReleaseComObject(array);
array = null;
}
} }
/// <summary>Returns an enumerator that iterates through the collection.</summary> /// <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> /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public override void Dispose() public override void Dispose()
{ {
if (lib != null) lib = null;
{
Marshal.ReleaseComObject(lib);
lib = null;
}
base.Dispose(); base.Dispose();
} }
@ -221,11 +217,7 @@ namespace Vanara.Windows.Shell
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary> /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public override void Dispose() public override void Dispose()
{ {
if (lib != null) lib = null;
{
Marshal.ReleaseComObject(lib);
lib = null;
}
base.Dispose(); base.Dispose();
} }

View File

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

View File

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

View File

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

View File

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

View File

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