ennorehling / csmapfx

CsMapFx is a GUI client for Eressea
https://www.eressea.de/
Other
2 stars 1 forks source link

new[]/delete[] -> unique_ptr #13

Closed Phygon closed 1 year ago

Phygon commented 1 year ago

An der Stelle würde ich statt new[] und delete[] einen unique_ptr benutzen. Ansonten würde der Speicher nicht freigegeben, falls assign eine Exception wirft. Das if(data) ist auch nicht notwendig, da new nie nullptrzurück gibt.

char* data = new char[size];
if (data) {
    if (PHYSFS_readBytes(file, data, filesize) == filesize) {
        result.assign(data, size);
    }
    delete[] data;
}

https://github.com/ennorehling/csmapfx/blob/master/fxhelper.cpp#L145

Neu:

auto data = std::make_unique<char[]>(size);
// oder: std::unique_ptr<char[]> data = std::make_unique<char[]>(size);
if (PHYSFS_readBytes(file, data.get(), filesize) == filesize) {
    result.assign(data.get(), size);
}
ennorehling commented 1 year ago

Das ging nur mit C++14, und bisher hatte ich das nicht gebraucht. Aber gut, wollen wir mal etwas mit der Zeit gehen.