Add missing destructors

v1
Brett 2023-07-27 01:53:01 -04:00
parent 126faa0b03
commit 6c7fab292c
2 changed files with 22 additions and 3 deletions

View File

@ -342,6 +342,10 @@ namespace blt::nbt {
t[v->getName()] = v; t[v->getName()] = v;
} }
} }
~tag_compound() override {
for (auto& v : t)
delete v.second;
}
}; };
static tag_t* _internal_::newCompound(){ static tag_t* _internal_::newCompound(){
@ -371,6 +375,9 @@ namespace blt::nbt {
} }
return dynamic_cast<T*>(tag); return dynamic_cast<T*>(tag);
} }
~NBTReader() {
delete root;
}
}; };
class NBTWriter { class NBTWriter {
@ -378,11 +385,20 @@ namespace blt::nbt {
blt::fs::block_writer& writer; blt::fs::block_writer& writer;
public: public:
explicit NBTWriter(blt::fs::block_writer& writer): writer(writer) {} explicit NBTWriter(blt::fs::block_writer& writer): writer(writer) {}
/**
* Write a compound tag and then DELETES the tag. If you don't wish for the memory to be freed, please use the reference version!
* @param root root NBT tag to write, this function assumes ownership of this pointer.
*/
void write(tag_compound* root){ void write(tag_compound* root){
writer.put((char)nbt_tag::COMPOUND); write(*root);
root->writeName(writer); delete root;
root->writePayload(writer);
} }
void write(tag_compound& root){
writer.put((char)nbt_tag::COMPOUND);
root.writeName(writer);
root.writePayload(writer);
}
~NBTWriter() = default;
}; };
} }

View File

@ -228,6 +228,9 @@ int main(int argc, char** argv) {
nbtWriter.write(new blt::nbt::tag_compound("root", { nbtWriter.write(new blt::nbt::tag_compound("root", {
new blt::nbt::tag_byte("super_byte", 8), new blt::nbt::tag_byte("super_byte", 8),
new blt::nbt::tag_short("shortTest", 32767), new blt::nbt::tag_short("shortTest", 32767),
new blt::nbt::tag_compound("SEXY_COMPOUND", {
})
})); }));
return 0; return 0;