Skip to content

Commit

Permalink
AttachEngine: fix crash when referenced objects get deleted
Browse files Browse the repository at this point in the history
... by verifying if the pointers equal to objects contained in all open
documents. Not terribly good, but I can't think of a situation where
doing this search might cause trouble.
  • Loading branch information
DeepSOIC committed May 13, 2016
1 parent 2d8a761 commit 71f70eb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Mod/Part/App/AttachEnginePyImp.cpp
Expand Up @@ -111,6 +111,7 @@ Py::Object AttachEnginePy::getReferences(void) const
{
try {
AttachEngine &attacher = *(this->getAttachEnginePtr());
AttachEngine::verifyReferencesAreSafe(attacher.references);
return Py::Object(attacher.references.getPyObject(),true);
} ATTACHERPY_STDCATCH_ATTR;
}
Expand Down Expand Up @@ -523,6 +524,7 @@ PyObject* AttachEnginePy::writeParametersToFeature(PyObject* args)
}
Part::AttachableObject* feat = static_cast<Part::AttachableObject*>(dobj);
const AttachEngine &attacher = *(this->getAttachEnginePtr());
AttachEngine::verifyReferencesAreSafe(attacher.references);
feat->Support.Paste(attacher.references);
feat->MapMode.setValue(attacher.mapMode);
feat->MapReversed.setValue(attacher.mapReverse);
Expand Down
20 changes: 20 additions & 0 deletions src/Mod/Part/App/Attacher.cpp
Expand Up @@ -61,6 +61,8 @@
#include "Attacher.h"
#include <Base/Console.h>
#include <App/OriginFeature.h>
#include <App/Application.h>
#include <App/Document.h>

using namespace Part;
using namespace Attacher;
Expand Down Expand Up @@ -749,6 +751,7 @@ void AttachEngine::readLinks(const App::PropertyLinkSubList &references,
std::vector<TopoDS_Shape> &storage,
std::vector<eRefType> &types)
{
verifyReferencesAreSafe(references);
const std::vector<App::DocumentObject*> &objs = references.getValues();
const std::vector<std::string> &sub = references.getSubValues();
geofs.resize(objs.size());
Expand Down Expand Up @@ -830,6 +833,23 @@ void AttachEngine::throwWrongMode(eMapMode mmode)
throw Base::Exception(errmsg.str().c_str());
}

void AttachEngine::verifyReferencesAreSafe(const App::PropertyLinkSubList &references)
{
const std::vector<App::DocumentObject*> links = references.getValues();
const std::vector<App::Document*> docs = App::GetApplication().getDocuments();
for(App::DocumentObject* lnk : links){
bool found = false;
for(App::Document* doc : docs){
if(doc->isIn(lnk)){
found = true;
}
}
if (!found){
throw Base::Exception("AttachEngine: verifyReferencesAreSafe: references point to deleted object.");
}
}
}


//=================================================================================

Expand Down
6 changes: 6 additions & 0 deletions src/Mod/Part/App/Attacher.h
Expand Up @@ -342,6 +342,12 @@ class PartExport AttachEngine : public Base::BaseClass

static GProp_GProps getInertialPropsOfShape(const std::vector<const TopoDS_Shape*> &shapes);

/**
* @brief verifyReferencesAreSafe: checks if pointers in references still
* point to objects contained in open documents. This guarantees the links
* are valid. Throws Base::Exception if invalid links are found.
*/
static void verifyReferencesAreSafe(const App::PropertyLinkSubList& references);

public: //enums
static const char* eMapModeStrings[];
Expand Down

0 comments on commit 71f70eb

Please sign in to comment.