From 7c29d7ca9f98c8929612d7bbc75c7b9a2f6fef58 Mon Sep 17 00:00:00 2001 From: Vulpovile Date: Tue, 8 Apr 2025 18:04:48 -0700 Subject: [PATCH] Fix memory error with reflection --- src/include/DataModelV3/PartInstance.h | 2 +- src/include/Enum.h | 20 +++++++++++-- src/include/Reflection/Reflection.h | 12 +------- src/include/Reflection/ReflectionProperty.h | 1 - .../Reflection/ReflectionProperty_impl.h | 30 ++++++++----------- src/source/DataModelV3/LevelInstance.cpp | 4 +-- src/source/DataModelV3/PVInstance.cpp | 4 +-- src/source/DataModelV3/PartInstance.cpp | 22 +++++++++----- src/source/Reflection/ReflectionDataTable.cpp | 10 +++---- 9 files changed, 55 insertions(+), 50 deletions(-) diff --git a/src/include/DataModelV3/PartInstance.h b/src/include/DataModelV3/PartInstance.h index 29923d1..ba3bf74 100644 --- a/src/include/DataModelV3/PartInstance.h +++ b/src/include/DataModelV3/PartInstance.h @@ -23,7 +23,7 @@ namespace B3D Reflection::ReflectionProperty shape; //OnTouch Reflection::ReflectionProperty onTouchAction; - Reflection::ReflectionProperty OnTouchSound; + Reflection::ReflectionProperty onTouchSound; //Non-Reflective Variables dBodyID physBody; diff --git a/src/include/Enum.h b/src/include/Enum.h index dfdfa4b..1c876da 100644 --- a/src/include/Enum.h +++ b/src/include/Enum.h @@ -2,16 +2,22 @@ namespace Enum { + struct EnumMeta { + int maxLength; + const char ** nameValues; + }; namespace SurfaceType { static const char* STR_TABLE[] = { "Smooth", "Bumps", "Hinge", "Motor", "StepperMotor", "Spawn" }; + enum Value { Smooth = 0, Bumps = 1, Hinge = 2, Motor = 3, StepperMotor = 4, Spawn = 5, LENGTH = 6 - }; - + }; + + static const EnumMeta ENUM_META = {LENGTH, STR_TABLE}; } namespace Shape { @@ -21,6 +27,8 @@ namespace Enum enum Value { Ball = 0, Block = 1, Cylinder = 2, LENGTH = 3 }; + + static const EnumMeta ENUM_META = {LENGTH, STR_TABLE}; } namespace Controller { @@ -31,6 +39,8 @@ namespace Enum enum Value { None = 0, KeyboardRight = 1, KeyboardLeft = 2, Joypad1 = 3, Joypad2 = 4, Chase = 5, Flee = 6, LENGTH=7 }; + + static const EnumMeta ENUM_META = {LENGTH, STR_TABLE}; } namespace ActionType { @@ -40,6 +50,8 @@ namespace Enum enum Value { Nothing = 0, Pause = 1, Lose = 2, Draw = 3, Win = 4, LENGTH = 5 }; + + static const EnumMeta ENUM_META = {LENGTH, STR_TABLE}; } namespace AffectType { @@ -49,6 +61,8 @@ namespace Enum enum Value { NoChange = 0, Increase = 1, Decrease = 2, LENGTH = 3 }; + + static const EnumMeta ENUM_META = {LENGTH, STR_TABLE}; } namespace Sound { @@ -63,5 +77,7 @@ namespace Enum Snap = 8, Page = 9, Click = 10, Clock = 11, Step = 12, StepOn = 13, LENGTH = 14 }; + + static const EnumMeta ENUM_META = {LENGTH, STR_TABLE}; } } \ No newline at end of file diff --git a/src/include/Reflection/Reflection.h b/src/include/Reflection/Reflection.h index e85e628..71c70ef 100644 --- a/src/include/Reflection/Reflection.h +++ b/src/include/Reflection/Reflection.h @@ -5,17 +5,6 @@ namespace B3D{ namespace Reflection{ //I do not like this... Structs? - class EnumMeta { - public: - EnumMeta(int maxLength, const char ** nameValues) - { - this->maxLength = maxLength; - this->nameValues = nameValues; - } - int maxLength; - const char ** nameValues; - }; - enum ReflectionType { TYPE_INT, TYPE_FLOAT, @@ -26,6 +15,7 @@ namespace B3D{ TYPE_CFRAME, TYPE_BOOLEAN, TYPE_ENUM, + TYPE_INVALID, LENGTH }; } diff --git a/src/include/Reflection/ReflectionProperty.h b/src/include/Reflection/ReflectionProperty.h index 8afc65c..3131817 100644 --- a/src/include/Reflection/ReflectionProperty.h +++ b/src/include/Reflection/ReflectionProperty.h @@ -32,7 +32,6 @@ namespace B3D{ void setValue(T); void setValueNotify(T); - void dispose(); //Too many #include "ReflectionProperty_op_overload.h" diff --git a/src/include/Reflection/ReflectionProperty_impl.h b/src/include/Reflection/ReflectionProperty_impl.h index 305f92a..db682f5 100644 --- a/src/include/Reflection/ReflectionProperty_impl.h +++ b/src/include/Reflection/ReflectionProperty_impl.h @@ -1,5 +1,17 @@ using namespace B3D::Reflection; +template +ReflectionProperty::ReflectionProperty(void) { + this->key = ""; + this->value = T(); + this->type = TYPE_INVALID; + this->containerTable = NULL; + this->locked = false; + this->archivable = true; + this->propertyHidden = false; + this->extData = NULL; +} + template ReflectionProperty::ReflectionProperty(std::string key, T * value, ReflectionType type, ReflectionDataTable * containerTable, void* extData = NULL, bool archivable = true, bool locked = false, bool propertyHidden = false) { @@ -28,15 +40,9 @@ ReflectionProperty::ReflectionProperty(std::string key, T value, ReflectionTy containerTable->mapProperty(key, (ReflectionProperty*)this); } -template -ReflectionProperty::ReflectionProperty(void) -{ -} - template ReflectionProperty::~ReflectionProperty(void) { - dispose(); } /* template @@ -59,18 +65,6 @@ void ReflectionProperty::setProperty(void) } } */ -template -void ReflectionProperty::dispose() -{ - //delete value; - //value = NULL; - if(extData) - { - //TODO why??? - //delete extData; - extData = NULL; - } -} template T ReflectionProperty::getValueClone() diff --git a/src/source/DataModelV3/LevelInstance.cpp b/src/source/DataModelV3/LevelInstance.cpp index 5755852..d4d8421 100644 --- a/src/source/DataModelV3/LevelInstance.cpp +++ b/src/source/DataModelV3/LevelInstance.cpp @@ -14,9 +14,9 @@ LevelInstance::LevelInstance(void) : Instance("LevelService") highScoreIsGood = Reflection::ReflectionProperty("HighScoreIsGood", false, TYPE_BOOLEAN, this->dataTable); runOnOpen = Reflection::ReflectionProperty("RunOnOpen", false, TYPE_BOOLEAN, this->dataTable); timerUpAction = Reflection::ReflectionProperty("TimerUpAction", Enum::ActionType::Nothing, TYPE_ENUM, this->dataTable, - new EnumMeta(Enum::ActionType::LENGTH, Enum::ActionType::STR_TABLE)); + (void*)&Enum::ActionType::ENUM_META); timerAffectsScore = Reflection::ReflectionProperty("TimerAffectsScore", Enum::AffectType::NoChange, TYPE_ENUM, this->dataTable, - new EnumMeta(Enum::AffectType::LENGTH, Enum::AffectType::STR_TABLE)); + (void*)&Enum::AffectType::ENUM_META); canDelete = false; } diff --git a/src/source/DataModelV3/PVInstance.cpp b/src/source/DataModelV3/PVInstance.cpp index f741870..b9e3fef 100644 --- a/src/source/DataModelV3/PVInstance.cpp +++ b/src/source/DataModelV3/PVInstance.cpp @@ -8,7 +8,7 @@ PVInstance::PVInstance(std::string className) : Instance(className) nameShown = Reflection::ReflectionProperty("NameShown", false, TYPE_BOOLEAN, this->dataTable); controllerFlagShown = Reflection::ReflectionProperty("ControllerFlagShown", true, TYPE_BOOLEAN, this->dataTable); controller = Reflection::ReflectionProperty("Controller", Enum::Controller::None, TYPE_ENUM, this->dataTable, - (void*)new EnumMeta(Enum::Controller::LENGTH, Enum::Controller::STR_TABLE)); + (void*)&Enum::Controller::ENUM_META); cFrame = Reflection::ReflectionProperty("CFrame", CoordinateFrame(), TYPE_CFRAME, this->dataTable); } @@ -20,7 +20,7 @@ PVInstance::PVInstance(void) : Instance("PVInstance") nameShown = Reflection::ReflectionProperty("NameShown", false, TYPE_BOOLEAN, this->dataTable); controllerFlagShown = Reflection::ReflectionProperty("ControllerFlagShown", true, TYPE_BOOLEAN, this->dataTable); controller = Reflection::ReflectionProperty("Controller", Enum::Controller::None, TYPE_ENUM, this->dataTable, - (void*)new EnumMeta(Enum::Controller::LENGTH, Enum::Controller::STR_TABLE)); + (void*)&Enum::Controller::ENUM_META); cFrame = Reflection::ReflectionProperty("CFrame", CoordinateFrame(), TYPE_CFRAME, this->dataTable); } diff --git a/src/source/DataModelV3/PartInstance.cpp b/src/source/DataModelV3/PartInstance.cpp index a30aa50..2f43f2b 100644 --- a/src/source/DataModelV3/PartInstance.cpp +++ b/src/source/DataModelV3/PartInstance.cpp @@ -24,19 +24,19 @@ PartInstance::PartInstance(void) : PVInstance("Part") velocity = ReflectionProperty("Velocity", Vector3(0,0,0), TYPE_VECTOR3, this->dataTable); rotVelocity = ReflectionProperty("RotVelocity", Vector3(0,0,0), TYPE_VECTOR3, this->dataTable); top = ReflectionProperty("TopSurface", Enum::SurfaceType::Bumps, TYPE_ENUM, this->dataTable, - (void*)new EnumMeta(Enum::SurfaceType::LENGTH, Enum::SurfaceType::STR_TABLE)); + (void*)&Enum::SurfaceType::ENUM_META); front = ReflectionProperty("FrontSurface", Enum::SurfaceType::Smooth, TYPE_ENUM, this->dataTable, - (void*)new EnumMeta(Enum::SurfaceType::LENGTH, Enum::SurfaceType::STR_TABLE)); + (void*)&Enum::SurfaceType::ENUM_META); right = ReflectionProperty("RightSurface", Enum::SurfaceType::Smooth, TYPE_ENUM, this->dataTable, - (void*)new EnumMeta(Enum::SurfaceType::LENGTH, Enum::SurfaceType::STR_TABLE)); + (void*)&Enum::SurfaceType::ENUM_META); back = ReflectionProperty("BackSurface", Enum::SurfaceType::Smooth, TYPE_ENUM, this->dataTable, - (void*)new EnumMeta(Enum::SurfaceType::LENGTH, Enum::SurfaceType::STR_TABLE)); + (void*)&Enum::SurfaceType::ENUM_META); left = ReflectionProperty("LeftSurface", Enum::SurfaceType::Smooth, TYPE_ENUM, this->dataTable, - (void*)new EnumMeta(Enum::SurfaceType::LENGTH, Enum::SurfaceType::STR_TABLE)); + (void*)&Enum::SurfaceType::ENUM_META); bottom = ReflectionProperty("BottomSurface", Enum::SurfaceType::Smooth, TYPE_ENUM, this->dataTable, - (void*)new EnumMeta(Enum::SurfaceType::LENGTH, Enum::SurfaceType::STR_TABLE)); + (void*)&Enum::SurfaceType::ENUM_META); shape = ReflectionProperty("Shape", Enum::Shape::Block, TYPE_ENUM, this->dataTable, - (void*)new EnumMeta(Enum::Shape::LENGTH, Enum::Shape::STR_TABLE)); + (void*)&Enum::Shape::ENUM_META); // OnTouch singleShot = ReflectionProperty("SingleShot", true, TYPE_BOOLEAN, this->dataTable); @@ -45,6 +45,12 @@ PartInstance::PartInstance(void) : PVInstance("Part") changeScore = ReflectionProperty("ChangeScore", 0, TYPE_INT, this->dataTable); changeTimer = ReflectionProperty("ChangeTimer", 0.0f, TYPE_FLOAT, this->dataTable); + + onTouchAction = Reflection::ReflectionProperty("OnTouchAction", Enum::ActionType::Nothing, TYPE_ENUM, this->dataTable, + (void*)&Enum::ActionType::ENUM_META); + onTouchSound = Reflection::ReflectionProperty("OnTouchSound", Enum::Sound::NoSound, TYPE_ENUM, this->dataTable, + (void*)&Enum::Sound::ENUM_META); + // Non-Reflective Properties physBody = NULL; glList = glGenLists(1); @@ -425,7 +431,7 @@ void PartInstance::onTouch() SoundService* sndService = getParentDataModel()->getSoundService(); - switch(OnTouchSound.getValue()) + switch(onTouchSound.getValue()) { case Enum::Sound::NoSound: break; diff --git a/src/source/Reflection/ReflectionDataTable.cpp b/src/source/Reflection/ReflectionDataTable.cpp index d9d5864..02c2088 100644 --- a/src/source/Reflection/ReflectionDataTable.cpp +++ b/src/source/Reflection/ReflectionDataTable.cpp @@ -15,11 +15,11 @@ ReflectionDataTable::ReflectionDataTable(void) ReflectionDataTable::~ReflectionDataTable(void) { - std::map*>::iterator it; - for (it = propertyTable.begin(); it != propertyTable.end(); it++) - { - it->second->dispose(); - } +// std::map*>::iterator it; +// for (it = propertyTable.begin(); it != propertyTable.end(); it++) +// { +// it->second->dispose(); +// } } std::string ReflectionDataTable::getClassName(void)