Skip to content

Commit

Permalink
Zinc: don't allocate our Zinc context on the heap.
Browse files Browse the repository at this point in the history
See the square-bracket comment at #2369 (comment).
  • Loading branch information
agarny committed Aug 13, 2020
1 parent deb5b82 commit 71a6078
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 28 deletions.
30 changes: 14 additions & 16 deletions src/plugins/test/ZincWindow/src/zincwindowwindow.cpp
Expand Up @@ -93,15 +93,13 @@ ZincWindowWindow::ZincWindowWindow(QWidget *pParent) :

ZincWindowWindow::~ZincWindowWindow()
{
// Delete the temporary copy of our .exfile file

QFile::remove(mExFile);
// Keep track of the fact that we are shutting down

// Delete some internal objects
mShuttingDown = true;

delete mZincContext;
// Delete the temporary copy of our .exfile file

mZincWidget = nullptr;
QFile::remove(mExFile);

// Delete the GUI

Expand All @@ -121,10 +119,10 @@ void ZincWindowWindow::retranslateUi()

void ZincWindowWindow::createAndSetZincContext()
{
// Make sure that we still have a Zinc widget (i.e. skip the case where we
// are coming here as a result of closing OpenCOR)
// Make sure that we are not shutting down (i.e. skip the case where we are
// coming here as a result of closing OpenCOR)

if (mZincWidget == nullptr) {
if (mShuttingDown) {
return;
}

Expand All @@ -134,10 +132,10 @@ void ZincWindowWindow::createAndSetZincContext()

// Create and set our Zinc context

mZincContext = new OpenCMISS::Zinc::Context("ZincWindowWindow");
mZincContext = OpenCMISS::Zinc::Context("ZincWindowWindow");

mZincContext->getMaterialmodule().defineStandardMaterials();
mZincContext->getGlyphmodule().defineStandardGlyphs();
mZincContext.getMaterialmodule().defineStandardMaterials();
mZincContext.getGlyphmodule().defineStandardGlyphs();

mZincWidget->setContext(mZincContext);

Expand All @@ -146,7 +144,7 @@ void ZincWindowWindow::createAndSetZincContext()
// Load our .exfile to our default region using a temporary copy of our
// .exfile file

OpenCMISS::Zinc::Region region = mZincContext->getDefaultRegion();
OpenCMISS::Zinc::Region region = mZincContext.getDefaultRegion();

region.readFile(mExFile.toUtf8().constData());

Expand All @@ -168,7 +166,7 @@ void ZincWindowWindow::createAndSetZincContext()
scene.beginChange();
// Black lines limiting our scene

OpenCMISS::Zinc::Materialmodule materialModule = mZincContext->getMaterialmodule();
OpenCMISS::Zinc::Materialmodule materialModule = mZincContext.getMaterialmodule();
OpenCMISS::Zinc::GraphicsLines lines = scene.createGraphicsLines();

lines.setCoordinateField(coordinates);
Expand Down Expand Up @@ -211,7 +209,7 @@ void ZincWindowWindow::createAndSetZincContext()

// Tesselation for our scene

OpenCMISS::Zinc::Tessellation fineTessellation = mZincContext->getTessellationmodule().createTessellation();
OpenCMISS::Zinc::Tessellation fineTessellation = mZincContext.getTessellationmodule().createTessellation();
int intValue = 8;

fineTessellation.setManaged(true);
Expand Down Expand Up @@ -259,7 +257,7 @@ void ZincWindowWindow::devicePixelRatioChanged(int pDevicePixelRatio)
{
// Update our scene using the given device pixel ratio

OpenCMISS::Zinc::Scene scene = mZincContext->getDefaultRegion().getScene();
OpenCMISS::Zinc::Scene scene = mZincContext.getDefaultRegion().getScene();

scene.beginChange();
scene.createGraphicsPoints().getGraphicspointattributes().getFont().setPointSize(pDevicePixelRatio*mAxesFontPointSize);
Expand Down
10 changes: 9 additions & 1 deletion src/plugins/test/ZincWindow/src/zincwindowwindow.h
Expand Up @@ -29,6 +29,12 @@ along with this program. If not, see <https://gnu.org/licenses>.

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

#include "zincbegin.h"
#include "opencmiss/zinc/context.hpp"
#include "zincend.h"

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

namespace Ui {
class ZincWindowWindow;
} // namespace Ui
Expand Down Expand Up @@ -70,8 +76,10 @@ class ZincWindowWindow : public Core::WindowWidget
private:
Ui::ZincWindowWindow *mGui;

bool mShuttingDown = false;

ZincWidget::ZincWidget *mZincWidget;
OpenCMISS::Zinc::Context *mZincContext = nullptr;
OpenCMISS::Zinc::Context mZincContext;

char *mZincSceneViewerDescription = nullptr;

Expand Down
15 changes: 7 additions & 8 deletions src/plugins/widget/ZincWidget/src/zincwidget.cpp
Expand Up @@ -32,7 +32,6 @@ along with this program. If not, see <https://gnu.org/licenses>.
//==============================================================================

#include "zincbegin.h"
#include "opencmiss/zinc/context.hpp"
#include "opencmiss/zinc/result.hpp"
#include "zincend.h"

Expand Down Expand Up @@ -80,7 +79,7 @@ ZincWidget::~ZincWidget()

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

OpenCMISS::Zinc::Context * ZincWidget::context() const
OpenCMISS::Zinc::Context ZincWidget::context() const
{
// Return our context

Expand All @@ -89,7 +88,7 @@ OpenCMISS::Zinc::Context * ZincWidget::context() const

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

void ZincWidget::setContext(OpenCMISS::Zinc::Context *pContext)
void ZincWidget::setContext(const OpenCMISS::Zinc::Context &pContext)
{
// Set our context

Expand Down Expand Up @@ -259,16 +258,16 @@ void ZincWidget::createSceneViewer()
// Create our scene viewer and have it have the same OpenGL properties as
// QOpenGLWidget

mSceneViewer = mContext->getSceneviewermodule().createSceneviewer(OpenCMISS::Zinc::Sceneviewer::BUFFERING_MODE_DOUBLE,
OpenCMISS::Zinc::Sceneviewer::STEREO_MODE_DEFAULT);
mSceneViewer = mContext.getSceneviewermodule().createSceneviewer(OpenCMISS::Zinc::Sceneviewer::BUFFERING_MODE_DOUBLE,
OpenCMISS::Zinc::Sceneviewer::STEREO_MODE_DEFAULT);

mSceneViewer.setProjectionMode(OpenCMISS::Zinc::Sceneviewer::PROJECTION_MODE_PERSPECTIVE);
mSceneViewer.setViewportSize(width(), height());

// Further customise our scene viewer

mSceneViewer.setScene(mContext->getDefaultRegion().getScene());
mSceneViewer.setScenefilter(mContext->getScenefiltermodule().getDefaultScenefilter());
mSceneViewer.setScene(mContext.getDefaultRegion().getScene());
mSceneViewer.setScenefilter(mContext.getScenefiltermodule().getDefaultScenefilter());

// Get ourselves a scene viewer notifier and set its callback to our
// callback class
Expand Down Expand Up @@ -325,7 +324,7 @@ void ZincWidget::initializeGL()

mGraphicsInitialized = true;

if (mContext != nullptr) {
if (mContext.isValid()) {
createSceneViewer();
}

Expand Down
6 changes: 3 additions & 3 deletions src/plugins/widget/ZincWidget/src/zincwidget.h
Expand Up @@ -86,8 +86,8 @@ class ZINCWIDGET_EXPORT ZincWidget : public QOpenGLWidget,
explicit ZincWidget(QWidget *pParent);
~ZincWidget() override;

OpenCMISS::Zinc::Context * context() const;
void setContext(OpenCMISS::Zinc::Context *pContext);
OpenCMISS::Zinc::Context context() const;
void setContext(const OpenCMISS::Zinc::Context &pContext);

OpenCMISS::Zinc::Sceneviewer sceneViewer() const;

Expand Down Expand Up @@ -127,7 +127,7 @@ class ZINCWIDGET_EXPORT ZincWidget : public QOpenGLWidget,

int mDevicePixelRatio = -1;

OpenCMISS::Zinc::Context *mContext = nullptr;
OpenCMISS::Zinc::Context mContext;
OpenCMISS::Zinc::Sceneviewer mSceneViewer;
OpenCMISS::Zinc::Sceneviewernotifier mSceneViewerNotifier;

Expand Down

0 comments on commit 71a6078

Please sign in to comment.