1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-09-17 16:50:26 +02:00

AUP3: UP-37 Crash with an empty project

And a bit of cleanup
This commit is contained in:
Leland Lucius 2020-07-20 12:51:01 -05:00
parent 84f363ee07
commit d48ec11578

View File

@ -944,6 +944,11 @@ sqlite3 *ProjectFileIO::CopyTo(const FilePath &destpath,
} }
} }
// Create the project doc
ProjectSerializer doc;
WriteXMLHeader(doc);
WriteXML(doc, false, tracks);
auto db = DB(); auto db = DB();
sqlite3 *destdb = nullptr; sqlite3 *destdb = nullptr;
bool success = false; bool success = false;
@ -1104,6 +1109,12 @@ sqlite3 *ProjectFileIO::CopyTo(const FilePath &destpath,
// Ensure attached DB connection gets configured // Ensure attached DB connection gets configured
Config(destdb, SafeConfig); Config(destdb, SafeConfig);
// Write the project doc
if (!WriteDoc("project", doc, destdb))
{
return nullptr;
}
// Tell cleanup everything is good to go // Tell cleanup everything is good to go
success = true; success = true;
@ -1168,8 +1179,8 @@ bool ProjectFileIO::ShouldVacuum(const std::shared_ptr<TrackList> &tracks)
// Let's make a percentage...should be plenty of head room // Let's make a percentage...should be plenty of head room
current *= 100; current *= 100;
wxLogDebug(wxT("used = %lld total = %lld %lld\n"), current, total, current / total); wxLogDebug(wxT("used = %lld total = %lld %lld\n"), current, total, total ? current / total : 0);
if (current / total > 80) if (!total || current / total > 80)
{ {
wxLogDebug(wxT("not vacuuming")); wxLogDebug(wxT("not vacuuming"));
return false; return false;
@ -1251,15 +1262,14 @@ void ProjectFileIO::Vacuum(const std::shared_ptr<TrackList> &tracks)
// Copy the original database to a new database while pruning unused sample blocks // Copy the original database to a new database while pruning unused sample blocks
auto newDB = CopyTo(origName, XO("Compacting project"), true, tracks); auto newDB = CopyTo(origName, XO("Compacting project"), true, tracks);
// Close handle to the original database // Close handle to the original database, even if the copy failed
CloseDB(); CloseDB();
// Reestablish the original name. No need to reopen as it will happen later, // Reestablish the original name.
// if needed.
UseConnection(newDB, origName); UseConnection(newDB, origName);
// If the copy failed or we aren't able to write the project doc, backout // If the copy failed or we aren't able to write the project doc, backout
if (!newDB || !WriteDoc("project", doc, newDB)) if (!newDB)
{ {
// Close the new database // Close the new database
sqlite3_close(newDB); sqlite3_close(newDB);
@ -1270,7 +1280,8 @@ void ProjectFileIO::Vacuum(const std::shared_ptr<TrackList> &tracks)
// AUD3 warn user somehow // AUD3 warn user somehow
wxRenameFile(tempName, origName); wxRenameFile(tempName, origName);
UseConnection(nullptr, origName); // Reopen original file
OpenDB(origName);
return; return;
} }
@ -2185,18 +2196,20 @@ bool ProjectFileIO::SaveProject(const FilePath &fileName)
// Install our checkpoint hook // Install our checkpoint hook
sqlite3_wal_hook(mDB, CheckpointHook, this); sqlite3_wal_hook(mDB, CheckpointHook, this);
} }
else
ProjectSerializer doc;
WriteXMLHeader(doc);
WriteXML(doc);
if (!WriteDoc("project", doc))
{ {
return false; ProjectSerializer doc;
} WriteXMLHeader(doc);
WriteXML(doc);
// Autosave no longer needed if (!WriteDoc("project", doc))
AutoSaveDelete(); {
return false;
}
// Autosave no longer needed
AutoSaveDelete();
}
// Reaching this point defines success and all the rest are no-fail // Reaching this point defines success and all the rest are no-fail
// operations: // operations:
@ -2227,17 +2240,6 @@ bool ProjectFileIO::SaveCopy(const FilePath& fileName)
return false; return false;
} }
ProjectSerializer doc;
WriteXMLHeader(doc);
WriteXML(doc);
// Write the project doc to the new DB
if (!WriteDoc("project", doc, db))
{
wxRemoveFile(fileName);
return false;
}
// All good...close the database // All good...close the database
(void) sqlite3_close(db); (void) sqlite3_close(db);