Skip to content

Commit d06ae49

Browse files
committed
Corrected segfault in standalone dimRed (problem with freeing memory).
1 parent 30a65fb commit d06ae49

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

core/base/dimensionReduction/DimensionReduction.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,25 @@
1515
using namespace std;
1616
using namespace ttk;
1717

18+
DimensionReduction::~DimensionReduction()
19+
{
20+
#ifdef TTK_ENABLE_SCIKIT_LEARN
21+
// If Python was not initialized by a still alive object.
22+
if (!this->wasPythonInitialized_) {
23+
Py_Finalize();
24+
}
25+
#endif
26+
}
27+
28+
29+
1830
DimensionReduction::DimensionReduction() {
1931
this->setDebugMsgPrefix("DimensionReduction");
2032

2133
#ifdef TTK_ENABLE_SCIKIT_LEARN
22-
auto finalize_callback = []() { Py_Finalize(); };
23-
2434
if(!Py_IsInitialized()) {
35+
this->wasPythonInitialized_ = false;
2536
Py_Initialize();
26-
atexit(finalize_callback);
2737
}
2838

2939
const char *version = Py_GetVersion();

core/base/dimensionReduction/DimensionReduction.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ namespace ttk {
5656
public:
5757
DimensionReduction();
5858

59+
~DimensionReduction();
60+
5961
/** Scikit-Learn Dimension Reduction algorithms */
6062
enum class METHOD {
6163
/** Spectral Embedding */
@@ -295,5 +297,8 @@ namespace ttk {
295297
int IsDeterministic{true};
296298
char majorVersion_{'0'};
297299
bool IsInputADistanceMatrix{false};
300+
301+
private:
302+
bool wasPythonInitialized_{true};
298303
};
299304
} // namespace ttk

0 commit comments

Comments
 (0)