From d38f673c6359cd8fe6ed983ce38454928ab1df33 Mon Sep 17 00:00:00 2001 From: DankParrot Date: Wed, 12 Aug 2020 20:00:17 -0700 Subject: [PATCH] NBT: Only call free() if we allocated it --- src/nbt/NBT.cpp | 7 ++++--- src/nbt/NBT.h | 9 +++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) 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();