diff --git a/include/blt/nbt/nbt.h b/include/blt/nbt/nbt.h index 273c7ad..f837c6b 100755 --- a/include/blt/nbt/nbt.h +++ b/include/blt/nbt/nbt.h @@ -342,6 +342,10 @@ namespace blt::nbt { t[v->getName()] = v; } } + ~tag_compound() override { + for (auto& v : t) + delete v.second; + } }; static tag_t* _internal_::newCompound(){ @@ -371,6 +375,9 @@ namespace blt::nbt { } return dynamic_cast(tag); } + ~NBTReader() { + delete root; + } }; class NBTWriter { @@ -378,11 +385,20 @@ namespace blt::nbt { blt::fs::block_writer& writer; public: 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){ - writer.put((char)nbt_tag::COMPOUND); - root->writeName(writer); - root->writePayload(writer); + write(*root); + delete root; } + void write(tag_compound& root){ + writer.put((char)nbt_tag::COMPOUND); + root.writeName(writer); + root.writePayload(writer); + } + ~NBTWriter() = default; }; } diff --git a/src/tests/main.cpp b/src/tests/main.cpp index 7694441..ff4b2de 100755 --- a/src/tests/main.cpp +++ b/src/tests/main.cpp @@ -228,6 +228,9 @@ int main(int argc, char** argv) { nbtWriter.write(new blt::nbt::tag_compound("root", { new blt::nbt::tag_byte("super_byte", 8), new blt::nbt::tag_short("shortTest", 32767), + new blt::nbt::tag_compound("SEXY_COMPOUND", { + + }) })); return 0;