From 6bb380c306894f4e2af7c114066842314f04927c Mon Sep 17 00:00:00 2001 From: abraunegg Date: Thu, 26 Oct 2023 06:23:05 +1100 Subject: [PATCH] Update main.d * Update exit scope handling, ensure that oneDriveApiInstance is shutdown otherwise memory leak occurs on exit PRECHANGE: ==442773== LEAK SUMMARY: ==442773== definitely lost: 0 bytes in 0 blocks ==442773== indirectly lost: 0 bytes in 0 blocks ==442773== possibly lost: 32 bytes in 1 blocks ==442773== still reachable: 175,810 bytes in 13 blocks ==442773== suppressed: 0 bytes in 0 blocks POSTCHANGE: ==450812== LEAK SUMMARY: ==450812== definitely lost: 0 bytes in 0 blocks ==450812== indirectly lost: 0 bytes in 0 blocks ==450812== possibly lost: 32 bytes in 1 blocks ==450812== still reachable: 153,856 bytes in 5 blocks ==450812== suppressed: 0 bytes in 0 blocks --- src/main.d | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/src/main.d b/src/main.d index c3250047e..359207d81 100644 --- a/src/main.d +++ b/src/main.d @@ -72,14 +72,14 @@ int main(string[] cliArgs) { // Detail what scope was called log.vdebug("Exit scope was called"); // Perform exit tasks - performStandardExitProcess(); + performStandardExitProcess("exitScope"); } scope(failure) { // Detail what scope was called log.vdebug("Failure scope was called"); // Perform exit tasks - performStandardExitProcess(); + performStandardExitProcess("failureScope"); } // Read in application options as passed in @@ -900,28 +900,51 @@ int main(string[] cliArgs) { } } -void performStandardExitProcess() { - // Was itemDB initialised? +void performStandardExitProcess(string scopeCaller) { + // Who called this function + log.vdebug("Running performStandardExitProcess due to: ", scopeCaller); + + // Shutdown the database if (itemDB !is null) { // Make sure the .wal file is incorporated into the main db before we exit itemDB.performVacuum(); object.destroy(itemDB); } - // Free other objects and memory + // Shutdown the OneDrive API instance + if (oneDriveApiInstance !is null) { + oneDriveApiInstance.shutdown(); + object.destroy(oneDriveApiInstance); + } + + // Shutdown the sync engine + if (syncEngineInstance !is null) object.destroy(syncEngineInstance); + + // Shutdown the client side filtering objects + if (selectiveSync !is null) object.destroy(selectiveSync); + + // Shutdown the application configuration objects if (appConfig !is null) { // Cleanup any existing dry-run elements ... these should never be left hanging around cleanupDryRunDatabaseFiles(appConfig.databaseFilePathDryRun); object.destroy(appConfig); } - if (oneDriveApiInstance !is null) object.destroy(oneDriveApiInstance); - if (selectiveSync !is null) object.destroy(selectiveSync); - if (syncEngineInstance !is null) object.destroy(syncEngineInstance); + // Shutdown any local filesystem monitoring if (filesystemMonitor !is null) { filesystemMonitor.shutdown(); object.destroy(filesystemMonitor); } + + if (scopeCaller == "failureScope") { + // Set these to be null due to failure scope - prevent 'ERROR: Unable to perform a database vacuum: out of memory' when the exit scope is then called + log.vdebug("Setting Class Objects to null due to failure scope"); + itemDB = null; + appConfig = null; + oneDriveApiInstance = null; + selectiveSync = null; + syncEngineInstance = null; + } } void performUploadOnlySyncProcess(string localPath, Monitor filesystemMonitor = null) {