Skip to content

Commit

Permalink
Update main.d
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
abraunegg committed Oct 25, 2023
1 parent a22e4e8 commit 6bb380c
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions src/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 6bb380c

Please sign in to comment.