diff --git a/devices/common/nvram.cpp b/devices/common/nvram.cpp index 0186c0e..04c398e 100644 --- a/devices/common/nvram.cpp +++ b/devices/common/nvram.cpp @@ -1,6 +1,6 @@ /* DingusPPC - The Experimental PowerPC Macintosh emulator -Copyright (C) 2018-21 divingkatae and maximum +Copyright (C) 2018-23 divingkatae and maximum (theweirdo) spatium (Contact divingkatae#1017 or powermax#2286 on Discord for more info) @@ -46,15 +46,14 @@ NVram::NVram(std::string file_name, uint32_t ram_size) this->file_name = file_name; this->ram_size = ram_size; - this->storage = new uint8_t[ram_size]; + // allocate memory storage and fill it with zeroes + this->storage = std::unique_ptr(new uint8_t[ram_size] ()); this->init(); } NVram::~NVram() { this->save(); - if (this->storage) - delete this->storage; } uint8_t NVram::read_byte(uint32_t offset) { @@ -74,9 +73,8 @@ void NVram::init() { if (f.fail() || !f.read(sig, sizeof(NVRAM_FILE_ID)) || !f.read((char*)&data_size, sizeof(data_size)) || memcmp(sig, NVRAM_FILE_ID, sizeof(NVRAM_FILE_ID)) || data_size != this->ram_size || - !f.read((char*)this->storage, this->ram_size)) { + !f.read((char*)this->storage.get(), this->ram_size)) { LOG_F(WARNING, "Could not restore NVRAM content from the given file."); - memset(this->storage, 0, sizeof(this->ram_size)); } f.close(); @@ -90,7 +88,7 @@ void NVram::save() { f.write((char*)&this->ram_size, sizeof(this->ram_size)); /* write NVRAM content */ - f.write((char*)this->storage, this->ram_size); + f.write((char*)this->storage.get(), this->ram_size); f.close(); } diff --git a/devices/common/nvram.h b/devices/common/nvram.h index d3e19bd..bb52540 100644 --- a/devices/common/nvram.h +++ b/devices/common/nvram.h @@ -1,6 +1,6 @@ /* DingusPPC - The Experimental PowerPC Macintosh emulator -Copyright (C) 2018-21 divingkatae and maximum +Copyright (C) 2018-23 divingkatae and maximum (theweirdo) spatium (Contact divingkatae#1017 or powermax#2286 on Discord for more info) @@ -49,7 +49,7 @@ public: private: std::string file_name; // file name for the backing file uint16_t ram_size; // NVRAM size - uint8_t* storage; + std::unique_ptr storage; void init(); void save();