NBT: Only call free() if we allocated it

This commit is contained in:
DankParrot 2020-08-12 20:00:17 -07:00
parent 4569c1429d
commit d38f673c63
2 changed files with 11 additions and 5 deletions

View File

@ -160,18 +160,19 @@ namespace NBT
//================================================== //==================================================
CompoundTag::CompoundTag(const char *filename) 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::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() CompoundTag::~CompoundTag()
{ {
nbt_free(m_node); if (m_isRoot)
nbt_free(m_node);
} }
const Tag CompoundTag::operator[](const char *name) const const Tag CompoundTag::operator[](const char *name) const

View File

@ -135,12 +135,17 @@ namespace NBT
// A Compound Tag is an unordered list of named tags // A Compound Tag is an unordered list of named tags
class CompoundTag : public Tag 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: public:
CompoundTag(nbt_node* node) : Tag(node) {} CompoundTag(nbt_node* node) : CompoundTag(node, false) {}
// Read tree from file
CompoundTag(const char* filename); CompoundTag(const char* filename);
// Read a compound tag from compressed data // Read tree from compressed data
CompoundTag(const void* data, size_t length); CompoundTag(const void* data, size_t length);
~CompoundTag(); ~CompoundTag();