Skip to content

Commit 5a3d45a

Browse files
committed
A workaround to detect dangling object references in Commands and
Interface. Now Object's destructor sets its id to 0xbadbeef, which makes it possible to detect dangling references.
1 parent 1610396 commit 5a3d45a

5 files changed

Lines changed: 17 additions & 5 deletions

File tree

src/lib/OGF/gom/lua/lua_graphite_object.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,9 +561,15 @@ namespace OGF {
561561

562562
Object* lua_tographite(lua_State* L, int index) {
563563
geo_debug_assert(lua_isgraphite(L,index));
564-
return static_cast<GraphiteRef*>(
564+
Object* result = static_cast<GraphiteRef*>(
565565
lua_touserdata(L,index)
566566
)->object;
567+
if(result->id() == Object::INVALID_ID) {
568+
Logger::warn("GOM") << "Reference to invalid Object"
569+
<< std::endl;
570+
result = nullptr;
571+
}
572+
return result;
567573
}
568574

569575

src/lib/OGF/gom/types/object.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ namespace OGF {
9090
id_to_object_ = nullptr;
9191
}
9292
}
93+
id_ = static_cast<unsigned int>(0xbadbeef);
9394
}
9495

9596
void Object::disconnect() {

src/lib/OGF/gom/types/object.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ namespace OGF {
6464
gom_attribute(abstract,"true")
6565
gom_class GOM_API Object : public Counted {
6666
public:
67+
static constexpr unsigned int INVALID_ID
68+
= static_cast<unsigned int>(0xbadbeef);
69+
6770
/**
6871
* \brief Object constructor.
6972
* \param[in] transient if true, the object is transient.

src/lib/OGF/scene_graph/commands/commands.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ namespace OGF {
6060
grob_ = grob ;
6161
}
6262

63+
Grob* Interface::get_grob() const {
64+
return ((grob_->id() == Object::INVALID_ID) ? nullptr : grob_);
65+
}
66+
6367
SceneGraph* Interface::scene_graph() const {
6468
return grob_->scene_graph() ;
6569
}
@@ -171,7 +175,7 @@ namespace OGF {
171175
}
172176

173177
Interpreter* Commands::interpreter() {
174-
if(get_grob() == nullptr) {
178+
if(get_grob() == nullptr || get_grob()->id() == Object::INVALID_ID) {
175179
return nullptr;
176180
}
177181
return get_grob()->interpreter();

src/lib/OGF/scene_graph/commands/commands.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,7 @@ namespace OGF {
111111
* \brief Gets the current Grob
112112
* \return a pointer to the current Grob
113113
*/
114-
Grob* get_grob() const {
115-
return grob_;
116-
}
114+
Grob* get_grob() const;
117115

118116
private:
119117
Grob* grob_;

0 commit comments

Comments
 (0)