From 2b5c34279c87ccbff253a167c95e111b985d2dbe Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 15 Feb 2018 22:20:27 -0600 Subject: [PATCH] Cleanup memory management in Project --- src/nostalgia/studio/lib/project.cpp | 18 ++++++------------ src/nostalgia/studio/lib/project.hpp | 6 ++++-- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/nostalgia/studio/lib/project.cpp b/src/nostalgia/studio/lib/project.cpp index 90f17e6e..f94b1c87 100644 --- a/src/nostalgia/studio/lib/project.cpp +++ b/src/nostalgia/studio/lib/project.cpp @@ -23,19 +23,15 @@ Project::Project(QString path) { } Project::~Project() { - if (m_fs) { - delete m_fs; - } } void Project::create() { - qDebug() << "Project::create"; QDir().mkpath(m_path); auto buffSize = 1024; auto buff = new uint8_t[buffSize]; FileSystem32::format(buff, buffSize, true); - m_fs = createFileSystem(buff, buffSize, true); + m_fs = std::unique_ptr{createFileSystem(buff, buffSize, true)}; QFile file(m_path + ROM_FILE); file.open(QIODevice::WriteOnly); @@ -46,23 +42,21 @@ void Project::create() { int Project::openRomFs() { QFile file(m_path + ROM_FILE); auto buffSize = file.size(); - auto buff = new uint8_t[buffSize]; + auto buff = std::make_unique(buffSize); if (file.exists()) { file.open(QIODevice::ReadOnly); - if (file.read((char*) buff, buffSize) > 0) { - m_fs = createFileSystem(buff, buffSize, true); + if (file.read((char*) buff.get(), buffSize) > 0) { + m_fs = std::unique_ptr{createFileSystem(buff.get(), buffSize, true)}; if (m_fs) { + buff.release(); return 0; } else { - delete []buff; return 1; } } else { - delete []buff; return 2; } } else { - delete []buff; return 3; } } @@ -77,7 +71,7 @@ int Project::saveRomFs() const { } FileSystem *Project::romFs() { - return m_fs; + return m_fs.get(); } int Project::mkdir(QString path) const { diff --git a/src/nostalgia/studio/lib/project.hpp b/src/nostalgia/studio/lib/project.hpp index 48ae6a06..7f8def3d 100644 --- a/src/nostalgia/studio/lib/project.hpp +++ b/src/nostalgia/studio/lib/project.hpp @@ -8,6 +8,8 @@ #pragma once +#include + #include #include @@ -23,7 +25,7 @@ class Project: public QObject { static QString ROM_FILE; QString m_path = ""; - ox::FileSystem *m_fs = nullptr; + std::unique_ptr m_fs; public: Project(QString path); @@ -63,7 +65,7 @@ int Project::writeObj(QString path, T *obj) const { // write MetalClaw size_t mcSize = 0; - err |= ox::write((uint8_t*) buff.data(), buffLen, obj, &mcSize); + err |= ox::writeMC((uint8_t*) buff.data(), buffLen, obj, &mcSize); if (err) { return err; }