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?)
This commit is contained in:
NT_x86
2023-03-27 12:06:57 +03:00
parent 9cee74baf4
commit f7a76511a8
7 changed files with 23 additions and 17 deletions

View File

@@ -25,6 +25,7 @@ public:
void removeChild(Instance*); void removeChild(Instance*);
void clearChildren(); void clearChildren();
Instance* getParent(); Instance* getParent();
void remove();
virtual Instance* clone() const { return new Instance(*this); } virtual Instance* clone() const { return new Instance(*this); }
virtual std::vector<PROPGRIDITEM> getProperties(); virtual std::vector<PROPGRIDITEM> getProperties();
virtual void PropUpdate(LPPROPGRIDITEM &pItem); virtual void PropUpdate(LPPROPGRIDITEM &pItem);

View File

@@ -8,6 +8,7 @@ class SnapInstance:
public: public:
SnapInstance(PartInstance* Part1, PartInstance* Part2); SnapInstance(PartInstance* Part1, PartInstance* Part2);
~SnapInstance(void); ~SnapInstance(void);
PartInstance* Joint1; PartInstance* jPart1;
PartInstance* Joint2; PartInstance* jPart2;
dJointID JointID;
}; };

View File

@@ -17,6 +17,6 @@ public:
void deleteBody(PartInstance* partInstance); void deleteBody(PartInstance* partInstance);
void updateBody(PartInstance* partInstance); void updateBody(PartInstance* partInstance);
void resetBody(PartInstance* partInstance); void resetBody(PartInstance* partInstance);
void createJoint(PartInstance *part1, PartInstance *part2); dJointID createJoint(PartInstance *part1, PartInstance *part2);
void destroyJoint(PartInstance *part); void destroyJoints(PartInstance *part);
}; };

View File

@@ -87,6 +87,7 @@ Instance::~Instance(void)
{ {
delete children.at(i); delete children.at(i);
} }
setParent(NULL);
} }
void Instance::setName(std::string newName) void Instance::setName(std::string newName)
@@ -174,5 +175,7 @@ Instance* Instance::findFirstChild(std::string name)
return NULL; return NULL;
} }
void Instance::remove()
{
delete this;
}

View File

@@ -20,16 +20,15 @@ void JointsService::createSnap(PartInstance* Part1, PartInstance* Part2)
Snap->setParent(this); Snap->setParent(this);
} }
//This is only for removing the Snap instance not for removing the joint
void JointsService::destroyPartSnap(PartInstance* Part) void JointsService::destroyPartSnap(PartInstance* Part)
{ {
std::vector<Instance* > children = getChildren(); std::vector<Instance* > children = getChildren();
for(size_t i = 0; i < children.size(); i++) for(size_t i = 0; i < children.size(); i++)
{ {
SnapInstance* Snap = (SnapInstance*)children.at(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();
} }
} }
} }

View File

@@ -8,8 +8,8 @@ SnapInstance::SnapInstance(PartInstance* Part1, PartInstance* Part2)
XplicitNgine* Phys = g_xplicitNgine; XplicitNgine* Phys = g_xplicitNgine;
name = "Snap"; name = "Snap";
className = "Snap"; className = "Snap";
Joint1 = Part1; jPart1 = Part1;
Joint2 = Part2; jPart2 = Part2;
if (Part1->physBody == NULL) if (Part1->physBody == NULL)
Phys->createBody(Part1); Phys->createBody(Part1);
@@ -17,13 +17,13 @@ SnapInstance::SnapInstance(PartInstance* Part1, PartInstance* Part2)
if (Part2->physBody == NULL) if (Part2->physBody == NULL)
Phys->createBody(Part2); Phys->createBody(Part2);
Phys->createJoint(Part1, Part2); JointID = Phys->createJoint(Part1, Part2);
} }
SnapInstance::~SnapInstance(void) SnapInstance::~SnapInstance(void)
{ {
XplicitNgine* Phys = g_xplicitNgine; //XplicitNgine* Phys = g_xplicitNgine;
printf("SnapInstance destroyed..."); printf("SnapInstance destroyed...");
Phys->destroyJoint(Joint1); if (JointID != NULL)
Phys->destroyJoint(Joint2); dJointDestroy(JointID);
} }

View File

@@ -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"); printf("XplicitNgine::createJoint called\n");
if((part1->physBody != NULL) & (part2->physBody != NULL)){ if((part1->physBody != NULL) & (part2->physBody != NULL)){
@@ -246,10 +246,12 @@ void XplicitNgine::createJoint(PartInstance *part1, PartInstance *part2)
dJointID c = dJointCreateFixed(physWorld, 0); dJointID c = dJointCreateFixed(physWorld, 0);
dJointAttach(c, part1->physBody, part2->physBody); dJointAttach(c, part1->physBody, part2->physBody);
dJointSetFixed(c); 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++) for(int i = 0; i < dBodyGetNumJoints(part->physBody); i++)
dJointDestroy(dBodyGetJoint(part->physBody, i)); dJointDestroy(dBodyGetJoint(part->physBody, i));