diff --git a/src/nbt/NBT.cpp b/src/nbt/NBT.cpp index 3ab830d..dd52449 100644 --- a/src/nbt/NBT.cpp +++ b/src/nbt/NBT.cpp @@ -160,18 +160,19 @@ namespace NBT //================================================== CompoundTag::CompoundTag(const char *filename) - : CompoundTag(nbt_parse_path(filename)) + : CompoundTag(nbt_parse_path(filename), true) // m_isRoot = true, this calls malloc { } CompoundTag::CompoundTag(const void* data, size_t length) - : CompoundTag(nbt_parse_compressed(data, length)) + : CompoundTag(nbt_parse_compressed(data, length), true) // m_isRoot = true, this calls malloc { } CompoundTag::~CompoundTag() { - nbt_free(m_node); + if (m_isRoot) + nbt_free(m_node); } const Tag CompoundTag::operator[](const char *name) const diff --git a/src/nbt/NBT.h b/src/nbt/NBT.h index 29f98b1..334b27c 100644 --- a/src/nbt/NBT.h +++ b/src/nbt/NBT.h @@ -135,12 +135,17 @@ namespace NBT // A Compound Tag is an unordered list of named tags class CompoundTag : public Tag { + // only true if the tree was allocated by constructor + const bool m_isRoot; + + CompoundTag(nbt_node* node, bool isRoot) : Tag(node), m_isRoot(false) {} public: - CompoundTag(nbt_node* node) : Tag(node) {} + CompoundTag(nbt_node* node) : CompoundTag(node, false) {} + // Read tree from file CompoundTag(const char* filename); - // Read a compound tag from compressed data + // Read tree from compressed data CompoundTag(const void* data, size_t length); ~CompoundTag();