From f7a76511a87d207e3dbc1be0c7acca87f38aa978 Mon Sep 17 00:00:00 2001 From: NT_x86 Date: Mon, 27 Mar 2023 12:06:57 +0300 Subject: [PATCH] Improved snap deletion and added remove function to instance class The current issue is that sometimes after deleting a snap and closing the game it throws an access violation (race condition?) --- src/include/DataModelV2/Instance.h | 1 + src/include/DataModelV2/SnapInstance.h | 5 +++-- src/include/XplicitNgine/XplicitNgine.h | 4 ++-- src/source/DataModelV2/Instance.cpp | 7 +++++-- src/source/DataModelV2/JointsService.cpp | 5 ++--- src/source/DataModelV2/SnapInstance.cpp | 12 ++++++------ src/source/XplicitNgine/XplicitNgine.cpp | 6 ++++-- 7 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/include/DataModelV2/Instance.h b/src/include/DataModelV2/Instance.h index be7eae1..e520204 100644 --- a/src/include/DataModelV2/Instance.h +++ b/src/include/DataModelV2/Instance.h @@ -25,6 +25,7 @@ public: void removeChild(Instance*); void clearChildren(); Instance* getParent(); + void remove(); virtual Instance* clone() const { return new Instance(*this); } virtual std::vector getProperties(); virtual void PropUpdate(LPPROPGRIDITEM &pItem); diff --git a/src/include/DataModelV2/SnapInstance.h b/src/include/DataModelV2/SnapInstance.h index 7a912d9..554b40b 100644 --- a/src/include/DataModelV2/SnapInstance.h +++ b/src/include/DataModelV2/SnapInstance.h @@ -8,6 +8,7 @@ class SnapInstance: public: SnapInstance(PartInstance* Part1, PartInstance* Part2); ~SnapInstance(void); - PartInstance* Joint1; - PartInstance* Joint2; + PartInstance* jPart1; + PartInstance* jPart2; + dJointID JointID; }; diff --git a/src/include/XplicitNgine/XplicitNgine.h b/src/include/XplicitNgine/XplicitNgine.h index 57237e0..4d2c7d2 100644 --- a/src/include/XplicitNgine/XplicitNgine.h +++ b/src/include/XplicitNgine/XplicitNgine.h @@ -17,6 +17,6 @@ public: void deleteBody(PartInstance* partInstance); void updateBody(PartInstance* partInstance); void resetBody(PartInstance* partInstance); - void createJoint(PartInstance *part1, PartInstance *part2); - void destroyJoint(PartInstance *part); + dJointID createJoint(PartInstance *part1, PartInstance *part2); + void destroyJoints(PartInstance *part); }; \ No newline at end of file diff --git a/src/source/DataModelV2/Instance.cpp b/src/source/DataModelV2/Instance.cpp index a83a9a8..4b9e4ad 100644 --- a/src/source/DataModelV2/Instance.cpp +++ b/src/source/DataModelV2/Instance.cpp @@ -87,6 +87,7 @@ Instance::~Instance(void) { delete children.at(i); } + setParent(NULL); } void Instance::setName(std::string newName) @@ -174,5 +175,7 @@ Instance* Instance::findFirstChild(std::string name) return NULL; } - - +void Instance::remove() +{ + delete this; +} \ No newline at end of file diff --git a/src/source/DataModelV2/JointsService.cpp b/src/source/DataModelV2/JointsService.cpp index cd9e89a..1c87544 100644 --- a/src/source/DataModelV2/JointsService.cpp +++ b/src/source/DataModelV2/JointsService.cpp @@ -20,16 +20,15 @@ void JointsService::createSnap(PartInstance* Part1, PartInstance* Part2) Snap->setParent(this); } -//This is only for removing the Snap instance not for removing the joint void JointsService::destroyPartSnap(PartInstance* Part) { std::vector children = getChildren(); for(size_t i = 0; i < children.size(); i++) { SnapInstance* Snap = (SnapInstance*)children.at(i); - if((Snap->Joint1 == Part) || (Snap->Joint2 == Part)) + if((Snap->jPart1 == Part) || (Snap->jPart2 == Part)) { - removeChild(Snap); + Snap->remove(); } } } \ No newline at end of file diff --git a/src/source/DataModelV2/SnapInstance.cpp b/src/source/DataModelV2/SnapInstance.cpp index 63edfdb..f0fd0ab 100644 --- a/src/source/DataModelV2/SnapInstance.cpp +++ b/src/source/DataModelV2/SnapInstance.cpp @@ -8,8 +8,8 @@ SnapInstance::SnapInstance(PartInstance* Part1, PartInstance* Part2) XplicitNgine* Phys = g_xplicitNgine; name = "Snap"; className = "Snap"; - Joint1 = Part1; - Joint2 = Part2; + jPart1 = Part1; + jPart2 = Part2; if (Part1->physBody == NULL) Phys->createBody(Part1); @@ -17,13 +17,13 @@ SnapInstance::SnapInstance(PartInstance* Part1, PartInstance* Part2) if (Part2->physBody == NULL) Phys->createBody(Part2); - Phys->createJoint(Part1, Part2); + JointID = Phys->createJoint(Part1, Part2); } SnapInstance::~SnapInstance(void) { - XplicitNgine* Phys = g_xplicitNgine; + //XplicitNgine* Phys = g_xplicitNgine; printf("SnapInstance destroyed..."); - Phys->destroyJoint(Joint1); - Phys->destroyJoint(Joint2); + if (JointID != NULL) + dJointDestroy(JointID); } diff --git a/src/source/XplicitNgine/XplicitNgine.cpp b/src/source/XplicitNgine/XplicitNgine.cpp index 449b7b0..46336df 100644 --- a/src/source/XplicitNgine/XplicitNgine.cpp +++ b/src/source/XplicitNgine/XplicitNgine.cpp @@ -238,7 +238,7 @@ void XplicitNgine::updateBody(PartInstance *partInstance) } -void XplicitNgine::createJoint(PartInstance *part1, PartInstance *part2) +dJointID XplicitNgine::createJoint(PartInstance *part1, PartInstance *part2) { printf("XplicitNgine::createJoint called\n"); if((part1->physBody != NULL) & (part2->physBody != NULL)){ @@ -246,10 +246,12 @@ void XplicitNgine::createJoint(PartInstance *part1, PartInstance *part2) dJointID c = dJointCreateFixed(physWorld, 0); dJointAttach(c, part1->physBody, part2->physBody); dJointSetFixed(c); + return c; } + return NULL; } -void XplicitNgine::destroyJoint(PartInstance *part) +void XplicitNgine::destroyJoints(PartInstance *part) { for(int i = 0; i < dBodyGetNumJoints(part->physBody); i++) dJointDestroy(dBodyGetJoint(part->physBody, i));