diff --git a/include/syntax/Field.hpp b/include/syntax/Field.hpp index e29e150d..300ab6c5 100644 --- a/include/syntax/Field.hpp +++ b/include/syntax/Field.hpp @@ -6,6 +6,7 @@ namespace geode::modifier { class FieldContainer : public cocos2d::CCObject { std::vector m_containedFields; + std::vector> m_destructorFunctions; public: static FieldContainer* create() { @@ -17,18 +18,21 @@ namespace geode::modifier { return nullptr; } ~FieldContainer() { - for(auto ptr : m_containedFields) { - operator delete(ptr); + for(auto i = 0; i < m_containedFields.size(); ++i) { + m_destructorFunctions[i](); + operator delete(m_containedFields[i]); } } void* getField(size_t index) { if (m_containedFields.size() <= index) { m_containedFields.resize(index + 1); + m_destructorFunctions.resize(index + 1); } return m_containedFields.at(index); } - void* setField(size_t index, size_t size) { + void* setField(size_t index, size_t size, std::function destructor) { m_containedFields.at(index) = operator new(size); + m_destructorFunctions.at(index) = destructor; return m_containedFields.at(index); } }; @@ -42,7 +46,7 @@ namespace geode::modifier { template > Parent* operator->() { // get the this pointer of the base - auto node = reinterpret_cast(reinterpret_cast(this) - sizeof(Base)); + auto node = reinterpret_cast(reinterpret_cast(this) - sizeof(Base)); auto container = reinterpret_cast(node->getUserObject()); if (!container) { container = FieldContainer::create(); @@ -51,7 +55,12 @@ namespace geode::modifier { static size_t index = Loader::get()->getFieldIndexForClass(typeid(Base).hash_code()); // this pointer is offset auto offsetField = container->getField(index); - if (!offsetField) offsetField = container->setField(index, sizeof(Parent) - sizeof(Intermediate)); + if (!offsetField) { + offsetField = container->setField(index, sizeof(Parent) - sizeof(Intermediate), + std::bind(&Parent::fieldDestructor, node) + ); + node->fieldConstructor(); + } return reinterpret_cast(reinterpret_cast(offsetField) - sizeof(Intermediate)); } diff --git a/include/syntax/InternalMacros.hpp b/include/syntax/InternalMacros.hpp index 63af650f..f082a304 100644 --- a/include/syntax/InternalMacros.hpp +++ b/include/syntax/InternalMacros.hpp @@ -39,6 +39,8 @@ template <> struct GEODE_HIDDEN _##derived : base { _##derived, \ _##derived \ > m_fields; \ + void fieldConstructor() {} \ + void fieldDestructor() {} \ }; \ template <> struct GEODE_HIDDEN _##derived : _##derived \