From 488b4854b2b866b317482a29a13bb5722f90f892 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 21 Jan 2019 17:36:40 +0000 Subject: [PATCH] Added reset of the State::CurrentVertexArrayState() to prevent the State::_vas becoming a dangling pointer when VertexArrayState objects are deleted. --- include/osg/State | 3 +++ include/osg/VertexArrayState | 2 ++ src/osg/VertexArrayState.cpp | 13 +++++++++++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/include/osg/State b/include/osg/State index d795ae88a..d003e6bb9 100644 --- a/include/osg/State +++ b/include/osg/State @@ -549,6 +549,9 @@ class OSG_EXPORT State : public Referenced /** Set the getCurrentVertexArrayState to the GlobalVertexArrayState.*/ void setCurrentToGlobalVertexArrayState() { _vas = _globalVertexArrayState.get(); } + /** Reset the CurrentVertexArrayObject if it's value equals the specificied vas - use when deleting a vas.*/ + void resetCurrentVertexArrayStateOnMatch(VertexArrayState* vas) { if (_vas==vas) _vas = 0; } + /** disable the vertex, normal, color, tex coords, secondary color, fog coord and index arrays.*/ void disableAllVertexArrays(); diff --git a/include/osg/VertexArrayState b/include/osg/VertexArrayState index 5b66db4a9..ef717719b 100644 --- a/include/osg/VertexArrayState +++ b/include/osg/VertexArrayState @@ -182,10 +182,12 @@ class OSG_EXPORT VertexArrayState : public osg::Referenced public: + virtual ~VertexArrayState(); // osg::GLBufferObject* getGLBufferObject(osg::Array* array); osg::State* _state; + osg::ref_ptr _stateObserverSet; osg::ref_ptr _ext; bool _isVertexBufferObjectSupported; diff --git a/src/osg/VertexArrayState.cpp b/src/osg/VertexArrayState.cpp index a644a2e2c..b3c4943d3 100644 --- a/src/osg/VertexArrayState.cpp +++ b/src/osg/VertexArrayState.cpp @@ -488,7 +488,7 @@ struct VertexAttribArrayDispatch : public VertexArrayState::ArrayDispatch virtual void enable_and_dispatch(osg::State& state, GLint size, GLenum type, GLsizei stride, const GLvoid *ptr, GLboolean normalized) { GLExtensions* ext = state.get(); - + ext->glEnableVertexAttribArray( unit ); ext->glVertexAttribPointer(static_cast(unit), size, type, normalized, stride, ptr); } @@ -526,10 +526,19 @@ VertexArrayState::VertexArrayState(osg::State* state): _currentEBO(0), _requiresSetArrays(true) { + _stateObserverSet = _state->getOrCreateObserverSet(); _ext = _state->get(); _isVertexBufferObjectSupported = _ext->isBufferObjectSupported; } +VertexArrayState::~VertexArrayState() +{ + if (_stateObserverSet->getObserverdObject()) + { + _state->resetCurrentVertexArrayStateOnMatch(this); + } +} + void VertexArrayState::generateVertexArrayObject() { _ext->glGenVertexArrays(1, &_vertexArrayObject); @@ -539,7 +548,7 @@ void VertexArrayState::deleteVertexArrayObject() { if (_vertexArrayObject) { - VAS_NOTICE<<" VertexArrayState::deleteVertexArrayObject() "<<_vertexArrayObject<getObserverdObject()<glDeleteVertexArrays(1, &_vertexArrayObject); _vertexArrayObject = 0;