Skip to content

Commit 7d82b22

Browse files
committed
Remove static instance 'NullScene' - fix memory error on pyluxcore exit
1 parent 6163f3a commit 7d82b22

File tree

9 files changed

+24
-27
lines changed

9 files changed

+24
-27
lines changed

include/slg/engines/caches/photongi/photongicache.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ class PhotonGICache {
267267
PhotonGICache(SceneConstRef scn, const PhotonGICacheParams &params);
268268
virtual ~PhotonGICache();
269269

270-
void SetScene(SceneRef scn) { scene = scn; }
270+
void SetScene(SceneRef scn) { scene = &scn; }
271271
PhotonGIDebugType GetDebugType() const { return params.debugType; }
272272

273273
bool IsIndirectEnabled() const { return params.indirect.enabled; }
@@ -337,7 +337,7 @@ class PhotonGICache {
337337

338338
template<class Archive> void serialize(Archive &ar, const u_int version);
339339

340-
std::reference_wrapper<const Scene> scene;
340+
SceneConstPtr scene;
341341

342342
PhotonGICacheParams params;
343343

include/slg/renderconfig.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ class RenderConfig {
118118
);
119119

120120
// Accessors
121-
SceneConstRef GetScene() const { return sceneRef; }
122-
SceneRef GetScene() { return sceneRef; }
121+
SceneConstRef GetScene() const { return *sceneRef; }
122+
SceneRef GetScene() { return *sceneRef; }
123123
PropertiesConstRef GetConfig() const { return *cfg; }
124124
PropertiesRef GetConfig() { return *cfg; }
125125
PropertiesRPtr GetConfigPtr() { return cfg; }
@@ -161,7 +161,7 @@ class RenderConfig {
161161
// RenderConfig constructor will generate an internal scene, and sceneRef will
162162
// be set to reference this internal scene
163163
SceneUPtr internalScene;
164-
std::reference_wrapper<Scene> sceneRef;
164+
ScenePtr sceneRef;
165165
};
166166

167167
} // namespace slg

include/slg/scene/scene.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,6 @@ class Scene {
369369
BOOST_SERIALIZATION_SPLIT_MEMBER()
370370
};
371371

372-
extern slg::Scene NullScene;
373-
374372
} // namespace slg
375373

376374
BOOST_CLASS_VERSION(slg::Scene, 1)

src/slg/engines/caches/photongi/pgicradius.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ float PhotonGICache::EvaluateBestRadius() {
5656
PGICFilm2SceneRadiusValidator validator(*this);
5757

5858
return Film2SceneRadius(
59-
scene, imagePlaneRadius, defaultRadius,
59+
*scene, imagePlaneRadius, defaultRadius,
6060
params.photon.maxPathDepth,
6161
params.photon.timeStart, params.photon.timeEnd,
6262
&validator);

src/slg/engines/caches/photongi/pgicvisibility.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class PGICSceneVisibility : public SceneVisibility<PGICVisibilityParticle> {
3434
public:
3535
PGICSceneVisibility(PhotonGICache &cache) :
3636
SceneVisibility(
37-
cache.scene,
37+
*cache.scene,
3838
cache.visibilityParticles,
3939
cache.params.photon.maxPathDepth,
4040
cache.params.visibility.maxSampleCount,

src/slg/engines/caches/photongi/photongicache.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ using namespace slg;
4040
//------------------------------------------------------------------------------
4141

4242
PhotonGICache::PhotonGICache() :
43-
scene(slg::NullScene),
43+
scene(nullptr),
4444
visibilityParticlesKdTree(nullptr),
4545
radiancePhotonsBVH(nullptr) ,
4646
causticPhotonsBVH(nullptr) {
4747
}
4848

4949
PhotonGICache::PhotonGICache(SceneConstRef scn, const PhotonGICacheParams &p) :
50-
scene(scn), params(p),
50+
scene(&scn), params(p),
5151
visibilityParticlesKdTree(nullptr),
5252
radiancePhotonsBVH(nullptr) ,
5353
indirectPhotonTracedCount(0),

src/slg/engines/caches/photongi/tracephotonsthread.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ bool TracePhotonsThread::TracePhotonPath(RandomGenerator &rndGen,
117117
newCausticPhotons.clear();
118118
vector<u_int> allNearEntryIndices;
119119

120-
SceneConstRef scene = pgic.scene;
120+
SceneConstRef scene = *pgic.scene;
121121
auto& camera = scene.GetCamera();
122122

123123
bool usefulPath = false;

src/slg/renderconfig.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <boost/algorithm/string.hpp>
2626
#include <boost/serialization/shared_ptr.hpp>
2727
#include <boost/serialization/unique_ptr.hpp>
28+
#include <boost/serialization/optional.hpp>
2829

2930
#include "luxrays/usings.h"
3031
#include "luxrays/utils/serializationutils.h"
@@ -89,7 +90,7 @@ static std::unique_ptr<Properties> defaultProperties;
8990
RenderConfig::RenderConfig(Private p, PropertiesRPtr props, SceneRef scn)
9091
:
9192
cfg(std::make_unique<Properties>()),
92-
sceneRef(scn)
93+
sceneRef(&scn)
9394
{
9495
InitDefaultProperties();
9596

@@ -110,7 +111,7 @@ RenderConfig::RenderConfig(Private p, PropertiesRPtr props, SceneRef scn)
110111
RenderConfig::RenderConfig(Private p, PropertiesRPtr props)
111112
:
112113
cfg(std::make_unique<Properties>()),
113-
sceneRef(NullScene) // Temporary, awaiting scene construction
114+
sceneRef(nullptr) // Temporary, awaiting scene construction
114115
{
115116
InitDefaultProperties();
116117

@@ -129,7 +130,7 @@ RenderConfig::RenderConfig(Private p, PropertiesRPtr props)
129130
std::make_unique<Properties>(sceneFileName),
130131
props
131132
);
132-
sceneRef = *internalScene;
133+
sceneRef = internalScene.get();
133134

134135
if (!GetScene().HasCamera()) {
135136
throw std::runtime_error(
@@ -146,11 +147,11 @@ RenderConfig::RenderConfig(
146147
PropertiesUPtr&& p_cfg, SceneRef p_scn, SceneUPtr&& p_internalscene
147148
) :
148149
cfg(std::move(p_cfg)),
149-
sceneRef(p_scn),
150+
sceneRef(&p_scn),
150151
internalScene(std::move(p_internalscene))
151152
{
152153
if (internalScene) {
153-
sceneRef = *internalScene;
154+
sceneRef = internalScene.get();
154155
}
155156
}
156157

@@ -585,7 +586,7 @@ void slg::RenderConfig::save_construct_data(
585586
ar << t->internalScene;
586587

587588
// Save SceneRef (as a pointer)
588-
ar << & t->sceneRef;
589+
ar << t->sceneRef;
589590
}
590591

591592
template<class Archive>
@@ -601,7 +602,7 @@ void slg::RenderConfig::load_construct_data(
601602
SceneUPtr sptr;
602603
ar >> sptr;
603604

604-
Scene * sref; // Load reference as a pointer
605+
decltype(sceneRef) sref; // Load reference as a pointer
605606
ar >> sref;
606607

607608
// invoke inplace constructor to initialize instance of RenderConfig

src/slg/scene/scene.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,18 @@ using namespace slg;
5858
// Scene
5959
//------------------------------------------------------------------------------
6060

61-
Scene::Scene(luxrays::PropertiesRPtr resizePolicyProps) {
61+
Scene::Scene(luxrays::PropertiesRPtr resizePolicyProps) :
62+
randomImageMap(ImageMap::AllocRandomImageMap(512))
63+
{
6264
Init(resizePolicyProps);
6365
}
6466

6567
Scene::Scene(
6668
PropertiesRPtr scnProps,
6769
PropertiesRPtr resizePolicyProps
68-
) {
70+
) :
71+
randomImageMap(ImageMap::AllocRandomImageMap(512))
72+
{
6973
Init(resizePolicyProps);
7074

7175
Parse(scnProps);
@@ -852,10 +856,4 @@ string Scene::EncodeTriangleLightNamePrefix(const string &objectName) {
852856

853857
ImageMapConstSPtr Scene::GetRandomImageMap() const { return randomImageMap; }
854858

855-
namespace slg {
856-
// TODO This is a workaround to initialize references to scenes in default constructors.
857-
// Correct solution would be to use boost serialization support for references
858-
Scene NullScene;
859-
}
860-
861859
// vim: autoindent noexpandtab tabstop=4 shiftwidth=4

0 commit comments

Comments
 (0)