Skip to content

Commit

Permalink
Use eager COM cleanup; trim config files (#26)
Browse files Browse the repository at this point in the history
* Free DIA objects where needed

* Bump some Nugets to current latest

* Use Marshal.FinalReleaseComObject and GC.Collect always

* Trimming down app.config files etc.
  • Loading branch information
arvindshmicrosoft authored Feb 14, 2022
1 parent 6ffc3d2 commit 34a36c0
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 33 deletions.
11 changes: 7 additions & 4 deletions Engine/DiaUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@ internal DiaUtil(string pdbName) {
this._IDiaSession.findChildrenEx(this._IDiaSession.globalScope, SymTagEnum.SymTagFunction, null, 0, out IDiaEnumSymbols matchedSyms);
foreach (IDiaSymbol sym in matchedSyms) {
this._IDiaSession.findLinesByRVA(sym.relativeVirtualAddress, (uint)sym.length, out IDiaEnumLineNumbers enumLineNums);
Marshal.ReleaseComObject(sym);
Marshal.FinalReleaseComObject(sym);
if (enumLineNums.count > 0) {
// this PDB has at least 1 function with source info, so end the search
HasSourceInfo = true;
break;
}

Marshal.ReleaseComObject(enumLineNums);
Marshal.FinalReleaseComObject(enumLineNums);
}
Marshal.FinalReleaseComObject(matchedSyms);
}

public void Dispose() {
Expand Down Expand Up @@ -142,6 +143,7 @@ internal static string GetSourceInfo(IDiaEnumLineNumbers enumLineNums, bool pdbH
enumLineNums.Item(tmpOrdinal).sourceFile.fileName,
enumLineNums.Item(tmpOrdinal).lineNumber));

Marshal.FinalReleaseComObject(enumLineNums.Item(tmpOrdinal).sourceFile);
Marshal.FinalReleaseComObject(enumLineNums.Item(tmpOrdinal));
}
}
Expand All @@ -168,12 +170,13 @@ internal static string ProcessInlineFrames(string moduleName, bool useUndecorate
inlineFrame.findInlineeLinesByRVA(inlineRVA, 0, out IDiaEnumLineNumbers enumLineNums);
sbInline.Append("\t");
sbInline.Append(DiaUtil.GetSourceInfo(enumLineNums, pdbHasSourceInfo));
Marshal.FinalReleaseComObject(enumLineNums);
}
inlineeIndex++;
Marshal.ReleaseComObject(inlineFrame);
Marshal.FinalReleaseComObject(inlineFrame);
sbInline.AppendLine();
}
Marshal.ReleaseComObject(enumInlinees);
Marshal.FinalReleaseComObject(enumInlinees);
} catch (COMException) {
sbInline.AppendLine(" -- WARNING: Unable to process inline frames; maybe symbols are mismatched?");
} catch (System.ArgumentException) {
Expand Down
12 changes: 6 additions & 6 deletions Engine/SQLCallStackResolver.Engine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.Collections.Immutable, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Collections.Immutable.5.0.0\lib\net461\System.Collections.Immutable.dll</HintPath>
<Reference Include="System.Collections.Immutable, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Collections.Immutable.6.0.0\lib\net461\System.Collections.Immutable.dll</HintPath>
</Reference>
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
Expand All @@ -70,11 +70,11 @@
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.Reflection.Metadata, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Reflection.Metadata.5.0.0\lib\net461\System.Reflection.Metadata.dll</HintPath>
<Reference Include="System.Reflection.Metadata, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Reflection.Metadata.6.0.0\lib\net461\System.Reflection.Metadata.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.5.0.0\lib\net45\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Xml" />
</ItemGroup>
Expand Down
19 changes: 8 additions & 11 deletions Engine/StackResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ private string ResolveSymbols(Dictionary<string, DiaUtil> _diautils, string[] ca
Marshal.FinalReleaseComObject(enumAllLineNums.Item(tmpOrdinalInner));
}
}

Marshal.ReleaseComObject(tmpSym);
Marshal.FinalReleaseComObject(enumAllLineNums);
Marshal.FinalReleaseComObject(tmpSym);
}
Marshal.ReleaseComObject(matchedSyms);
Marshal.FinalReleaseComObject(matchedSyms);
}
}
}
Expand Down Expand Up @@ -240,6 +240,7 @@ private string ProcessFrameModuleOffset(Dictionary<string, DiaUtil> _diautils, r
if (includeSourceInfo) {
_diautils[moduleName]._IDiaSession.findLinesByRVA(rva, 0, out IDiaEnumLineNumbers enumLineNums);
sourceInfo = DiaUtil.GetSourceInfo(enumLineNums, pdbHasSourceInfo);
Marshal.FinalReleaseComObject(enumLineNums);
}
if (showInlineFrames && pdbHasSourceInfo && !sourceInfo.Contains("-- WARNING:")) {
inlineFrameAndSourceInfo = DiaUtil.ProcessInlineFrames(moduleName, useUndecorateLogic, includeOffset, includeSourceInfo, rva, mysym, pdbHasSourceInfo);
Expand Down Expand Up @@ -538,14 +539,8 @@ public string ResolveCallstacks(string inputCallstackText, string symPath, bool
}
}

// Unfortunately the below is necessary to ensure that the handles to the cached PDB files opened by DIA
// and later deleted at the next invocation of this function, are released deterministically
// This is despite we correctly releasing those interface pointers using Marshal.FinalReleaseComObject
// Thankfully we only need to resort to this if the caller wants to cache PDBs in the temp folder
if (cachePDB) {
GC.Collect();
GC.WaitForPendingFinalizers();
}
GC.Collect();
GC.WaitForPendingFinalizers();

this.StatusMessage = "Finished!";
if (string.IsNullOrEmpty(outputFilePath)) {
Expand Down Expand Up @@ -593,6 +588,8 @@ private void ProcessCallStack(Object obj) {
foreach (var diautil in _diautils.Values) {
diautil.Dispose();
}

_diautils.Clear();
}

SafeNativeMethods.DestroyActivationContext();
Expand Down
13 changes: 11 additions & 2 deletions Engine/app.config
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
<?xml version="1.0" encoding="utf-8"?><!--// Copyright (c) Microsoft Corporation. Licensed under the MIT License - see LICENSE file in this repo.
--><configuration><runtime><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /><bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" /></dependentAssembly><dependentAssembly><assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" /><bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" /></dependentAssembly><dependentAssembly><assemblyIdentity name="Microsoft.Diagnostics.NETCore.Client" publicKeyToken="31bf3856ad364e35" culture="neutral" /><bindingRedirect oldVersion="0.0.0.0-0.2.2.37102" newVersion="0.2.2.37102" /></dependentAssembly></assemblyBinding></runtime><startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" /></startup></configuration>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
14 changes: 12 additions & 2 deletions Engine/packages.config
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
<?xml version="1.0" encoding="utf-8"?><!--// Copyright (c) Microsoft Corporation. Licensed under the MIT License - see LICENSE file in this repo.
--><packages><package id="Microsoft.SqlServer.XEvent.XELite" version="2019.11.20.2" targetFramework="net472" /><package id="System.Buffers" version="4.5.1" targetFramework="net472" /><package id="System.Collections.Immutable" version="5.0.0" targetFramework="net472" /><package id="System.Memory" version="4.5.4" targetFramework="net472" /><package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net472" /><package id="System.Reflection.Metadata" version="5.0.0" targetFramework="net472" /><package id="System.Runtime.CompilerServices.Unsafe" version="5.0.0" targetFramework="net472" /></packages>
<?xml version="1.0" encoding="utf-8"?>
<!--// Copyright (c) Microsoft Corporation. Licensed under the MIT License - see LICENSE file in this repo.
-->
<packages>
<package id="Microsoft.SqlServer.XEvent.XELite" version="2019.11.20.2" targetFramework="net472" />
<package id="System.Buffers" version="4.5.1" targetFramework="net472" />
<package id="System.Collections.Immutable" version="6.0.0" targetFramework="net472" />
<package id="System.Memory" version="4.5.4" targetFramework="net472" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net472" />
<package id="System.Reflection.Metadata" version="6.0.0" targetFramework="net472" />
<package id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" targetFramework="net472" />
</packages>
13 changes: 11 additions & 2 deletions GUI/App.config
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
<?xml version="1.0" encoding="utf-8"?><!--// Copyright (c) Microsoft Corporation. Licensed under the MIT License - see LICENSE file in this repo.
--><configuration><startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" /></startup><appSettings><add key="PDBDownloadFolder" value="c:\temp" /><add key="SQLBuildInfoUpdateURLs" value="https://raw.githubusercontent.com/microsoft/SQLCallStackResolver/main/lastupdated.txt;https://raw.githubusercontent.com/arvindshmicrosoft/SQLCallStackResolver/main/lastupdated.txt" /><add key="SQLBuildInfoURLs" value="https://raw.githubusercontent.com/microsoft/SQLCallStackResolver/main/sqlbuildinfo.json;https://raw.githubusercontent.com/arvindshmicrosoft/SQLCallStackResolver/main/sqlbuildinfo.json" /></appSettings><runtime><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /><bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" /></dependentAssembly><dependentAssembly><assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" /><bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" /></dependentAssembly><dependentAssembly><assemblyIdentity name="Microsoft.Diagnostics.NETCore.Client" publicKeyToken="31bf3856ad364e35" culture="neutral" /><bindingRedirect oldVersion="0.0.0.0-0.2.2.37102" newVersion="0.2.2.37102" /></dependentAssembly></assemblyBinding></runtime></configuration>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<appSettings>
<add key="PDBDownloadFolder" value="c:\temp" />
<add key="SQLBuildInfoUpdateURLs" value="https://raw.githubusercontent.com/microsoft/SQLCallStackResolver/main/lastupdated.txt;https://raw.githubusercontent.com/arvindshmicrosoft/SQLCallStackResolver/main/lastupdated.txt" />
<add key="SQLBuildInfoURLs" value="https://raw.githubusercontent.com/microsoft/SQLCallStackResolver/main/sqlbuildinfo.json;https://raw.githubusercontent.com/arvindshmicrosoft/SQLCallStackResolver/main/sqlbuildinfo.json" />
</appSettings>
</configuration>
4 changes: 1 addition & 3 deletions GUI/SQLCallstackResolver.GUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<DependentUpon>SQLBuildsForm.cs</DependentUpon>
<SubType>Designer</SubType>
</EmbeddedResource>
<None Include="App.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand All @@ -100,9 +101,6 @@
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Engine\SQLCallstackResolver.Engine.csproj">
<Project>{782bbd60-ee45-43cd-8a4f-0505efe4ff1a}</Project>
Expand Down
1 change: 0 additions & 1 deletion Tests/SQLCallStackResolver.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config" />
<Content Include="xunit.runner.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
2 changes: 0 additions & 2 deletions Tests/app.config

This file was deleted.

0 comments on commit 34a36c0

Please sign in to comment.