<Fix>: Improve crash diagnostics, increase revision to 3.9.3147.102

Cause:

- The Out of memory error occurred while creating the WinForms error dialog, potentially masking the original exception.
- Crash logs lacked resource usage details needed to investigate memory or graphics resource exhaustion.

Solution:

1. Improved exception logging
   - Added first-chance logging for memory and native exceptions.
   - Included full exception details, HRESULT, native error code and termination status.
   - Protected logging against recursion and diagnostic failures while preserving existing exception handling.

2. Added resource diagnostics
   - Logged process memory usage, handle and thread counts.
   - Recorded current and peak GDI and USER object counts.
   - Included application version, thread ID and process architecture.

3. Increased revision
   - Updated AssemblyVersion and AssemblyFileVersion from 3.9.3147.101 to 3.9.3147.102.
This commit is contained in:
2026-09-24 08:31:30 +02:00
parent c53a2d9078
commit 1e0c65f213
2 changed files with 65 additions and 10 deletions
+63 -8
View File
@@ -5,6 +5,8 @@ using System;
using System.IO;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using log4net;
using Common;
@@ -31,6 +33,12 @@ namespace TBF
static ILog log;
const string log4netConfigFName = "log4netConfig.xml";
[ThreadStatic]
static bool loggingException;
[DllImport("user32.dll")]
static extern uint GetGuiResources(IntPtr process, uint flags);
/// Program information
public static string Version; /// version string
public static DateTime BuildDateTime; /// date and time of program build
@@ -70,6 +78,7 @@ namespace TBF
static void Main()
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);
AppDomain.CurrentDomain.FirstChanceException += OnFirstChanceException;
/// Program version string
System.Reflection.Assembly thisAssembly = System.Reflection.Assembly.GetExecutingAssembly();
@@ -521,19 +530,65 @@ namespace TBF
static void MyHandler(object sender, UnhandledExceptionEventArgs args)
{
LogException(log, "Unhandled exception", (Exception)args.ExceptionObject);
LogException(log, "Unhandled exception; IsTerminating=" + args.IsTerminating,
args.ExceptionObject as Exception);
}
static void OnFirstChanceException(object sender, FirstChanceExceptionEventArgs args)
{
// Observe resource failures before WinForms attempts to create its error dialog.
// First-chance exceptions may be handled later; this does not imply a crash.
if (args.Exception is OutOfMemoryException || args.Exception is ExternalException)
LogException(log, "First-chance resource/native exception (may be handled)", args.Exception);
}
static void LogException(ILog log, string description, Exception e)
{
log.FatalFormat("---------------( {0} )---------------", description);
log.FatalFormat("Message : {0}", e.Message);
if (e.InnerException != null)
// Diagnostics must neither recurse through FirstChanceException nor replace
// the original failure if memory or native resources are exhausted.
if (loggingException || log == null) return;
loggingException = true;
try
{
log.FatalFormat("InnerMessage : {0}", e.InnerException.Message);
log.FatalFormat("---------------( {0} )---------------", description);
log.FatalFormat("Thread={0}; Is64BitProcess={1}; Version={2}",
System.Threading.Thread.CurrentThread.ManagedThreadId,
Environment.Is64BitProcess, Version);
// ToString includes the exception type and inner exception stack traces.
log.FatalFormat("Exception: {0}", e);
if (e != null) log.FatalFormat("HResult: 0x{0:X8}", e.HResult);
var nativeException = e as System.ComponentModel.Win32Exception;
if (nativeException != null)
log.FatalFormat("NativeErrorCode: {0}", nativeException.NativeErrorCode);
try
{
using (var process = System.Diagnostics.Process.GetCurrentProcess())
{
log.FatalFormat("Resources: PID={0}; PrivateBytes={1}; WorkingSetBytes={2}; " +
"VirtualBytes={3}; ManagedBytes={4}; Handles={5}; Threads={6}",
process.Id, process.PrivateMemorySize64, process.WorkingSet64,
process.VirtualMemorySize64, GC.GetTotalMemory(false),
process.HandleCount, process.Threads.Count);
log.FatalFormat("GUI resources: GDI={0}; USER={1}; GDIPeak={2}; USERPeak={3}",
GetGuiResources(process.Handle, 0), GetGuiResources(process.Handle, 1),
GetGuiResources(process.Handle, 2), GetGuiResources(process.Handle, 4));
}
}
catch
{
// Resource collection is best effort during a resource failure.
}
log.Fatal("--------------------------------------");
}
catch
{
// Preserve the original exception even if logging itself fails.
}
finally
{
loggingException = false;
}
log.FatalFormat("StackTrace : {0}{1}", Environment.NewLine, e.StackTrace);
log.Fatal("--------------------------------------");
}
@@ -566,4 +621,4 @@ namespace TBF
formThread = null;
}
}
}
}
+2 -2
View File
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// Build Number
// Revision
//
[assembly: AssemblyVersion("3.9.3147.101")]
[assembly: AssemblyFileVersion("3.9.3147.101")]
[assembly: AssemblyVersion("3.9.3147.102")]
[assembly: AssemblyFileVersion("3.9.3147.102")]