2020
2121namespace itk ::fem
2222{
23+ // Destructor is defined out-of-line so that the vtable and typeinfo are
24+ // emitted as exported symbols in shared library builds. Moving it to
25+ // inline = default in the header would make them hidden under
26+ // -fvisibility-inlines-hidden, breaking dynamic_cast across DSO boundaries.
27+ // See: https://github.com/InsightSoftwareConsortium/ITK/issues/6000
28+ LinearSystemWrapperDenseVNL::~LinearSystemWrapperDenseVNL () = default ;
29+
2330void
2431LinearSystemWrapperDenseVNL::InitializeMatrix (unsigned int matrixIndex)
2532{
2633 // allocate if necessary
2734 if (m_Matrices == nullptr )
2835 {
29- m_Matrices = new MatrixHolder (m_NumberOfMatrices);
36+ m_Matrices = std::make_unique< MatrixHolder> (m_NumberOfMatrices);
3037 }
3138
32- // out with old, in with new
33- delete (*m_Matrices)[matrixIndex];
34-
35- (*m_Matrices)[matrixIndex] = new MatrixRepresentation (this ->GetSystemOrder (), this ->GetSystemOrder ());
39+ (*m_Matrices)[matrixIndex] = std::make_unique<MatrixRepresentation>(this ->GetSystemOrder (), this ->GetSystemOrder ());
3640 (*m_Matrices)[matrixIndex]->fill (0.0 );
3741}
3842
@@ -56,8 +60,7 @@ LinearSystemWrapperDenseVNL::DestroyMatrix(unsigned int matrixIndex)
5660{
5761 if (m_Matrices)
5862 {
59- delete (*m_Matrices)[matrixIndex];
60- (*m_Matrices)[matrixIndex] = nullptr ;
63+ (*m_Matrices)[matrixIndex].reset ();
6164 }
6265}
6366
@@ -67,13 +70,10 @@ LinearSystemWrapperDenseVNL::InitializeVector(unsigned int vectorIndex)
6770 // allocate if necessary
6871 if (m_Vectors == nullptr )
6972 {
70- m_Vectors = new std::vector<vnl_vector<Float> * >(m_NumberOfVectors);
73+ m_Vectors = std::make_unique<std:: vector<std::unique_ptr< vnl_vector<Float>>> >(m_NumberOfVectors);
7174 }
7275
73- // out with old, in with new
74- delete (*m_Vectors)[vectorIndex];
75-
76- (*m_Vectors)[vectorIndex] = new vnl_vector<Float>(this ->GetSystemOrder ());
76+ (*m_Vectors)[vectorIndex] = std::make_unique<vnl_vector<Float>>(this ->GetSystemOrder ());
7777 (*m_Vectors)[vectorIndex]->fill (0.0 );
7878}
7979
@@ -97,8 +97,7 @@ LinearSystemWrapperDenseVNL::DestroyVector(unsigned int vectorIndex)
9797{
9898 if (m_Vectors)
9999 {
100- delete (*m_Vectors)[vectorIndex];
101- (*m_Vectors)[vectorIndex] = nullptr ;
100+ (*m_Vectors)[vectorIndex].reset ();
102101 }
103102}
104103
@@ -108,13 +107,10 @@ LinearSystemWrapperDenseVNL::InitializeSolution(unsigned int solutionIndex)
108107 // allocate if necessary
109108 if (m_Solutions == nullptr )
110109 {
111- m_Solutions = new std::vector<vnl_vector<Float> * >(m_NumberOfSolutions);
110+ m_Solutions = std::make_unique<std:: vector<std::unique_ptr< vnl_vector<Float>>> >(m_NumberOfSolutions);
112111 }
113112
114- // out with old, in with new
115- delete (*m_Solutions)[solutionIndex];
116-
117- (*m_Solutions)[solutionIndex] = new vnl_vector<Float>(this ->GetSystemOrder ());
113+ (*m_Solutions)[solutionIndex] = std::make_unique<vnl_vector<Float>>(this ->GetSystemOrder ());
118114 (*m_Solutions)[solutionIndex]->fill (0.0 );
119115}
120116
@@ -138,8 +134,7 @@ LinearSystemWrapperDenseVNL::DestroySolution(unsigned int solutionIndex)
138134{
139135 if (m_Solutions)
140136 {
141- delete (*m_Solutions)[solutionIndex];
142- (*m_Solutions)[solutionIndex] = nullptr ;
137+ (*m_Solutions)[solutionIndex].reset ();
143138 }
144139}
145140
@@ -189,39 +184,31 @@ LinearSystemWrapperDenseVNL::Solve()
189184void
190185LinearSystemWrapperDenseVNL::SwapMatrices (unsigned int MatrixIndex1, unsigned int MatrixIndex2)
191186{
192- vnl_matrix<Float> * tmp = (*m_Matrices)[MatrixIndex1];
193- (*m_Matrices)[MatrixIndex1] = (*m_Matrices)[MatrixIndex2];
194- (*m_Matrices)[MatrixIndex2] = tmp;
187+ std::swap ((*m_Matrices)[MatrixIndex1], (*m_Matrices)[MatrixIndex2]);
195188}
196189
197190void
198191LinearSystemWrapperDenseVNL::SwapVectors (unsigned int VectorIndex1, unsigned int VectorIndex2)
199192{
200- vnl_vector<Float> tmp = *(*m_Vectors)[VectorIndex1];
201- *(*m_Vectors)[VectorIndex1] = *(*m_Vectors)[VectorIndex2];
202- *(*m_Vectors)[VectorIndex2] = tmp;
193+ std::swap ((*m_Vectors)[VectorIndex1], (*m_Vectors)[VectorIndex2]);
203194}
204195
205196void
206197LinearSystemWrapperDenseVNL::SwapSolutions (unsigned int SolutionIndex1, unsigned int SolutionIndex2)
207198{
208- vnl_vector<Float> * tmp = (*m_Solutions)[SolutionIndex1];
209- (*m_Solutions)[SolutionIndex1] = (*m_Solutions)[SolutionIndex2];
210- (*m_Solutions)[SolutionIndex2] = tmp;
199+ std::swap ((*m_Solutions)[SolutionIndex1], (*m_Solutions)[SolutionIndex2]);
211200}
212201
213202void
214203LinearSystemWrapperDenseVNL::CopySolution2Vector (unsigned int SolutionIndex, unsigned int VectorIndex)
215204{
216- delete (*m_Vectors)[VectorIndex];
217- (*m_Vectors)[VectorIndex] = new vnl_vector<Float>(*((*m_Solutions)[SolutionIndex]));
205+ (*m_Vectors)[VectorIndex] = std::make_unique<vnl_vector<Float>>(*((*m_Solutions)[SolutionIndex]));
218206}
219207
220208void
221209LinearSystemWrapperDenseVNL::CopyVector2Solution (unsigned int VectorIndex, unsigned int SolutionIndex)
222210{
223- delete (*m_Solutions)[SolutionIndex];
224- (*m_Solutions)[SolutionIndex] = new vnl_vector<Float>(*((*m_Vectors)[VectorIndex]));
211+ (*m_Solutions)[SolutionIndex] = std::make_unique<vnl_vector<Float>>(*((*m_Vectors)[VectorIndex]));
225212}
226213
227214void
@@ -266,24 +253,4 @@ LinearSystemWrapperDenseVNL::ScaleSolution(Float scale, unsigned int solutionInd
266253 (*(*m_Solutions)[solutionIndex]) = (*(*m_Solutions)[solutionIndex]) * scale;
267254}
268255
269- LinearSystemWrapperDenseVNL::~LinearSystemWrapperDenseVNL ()
270- {
271- for (unsigned int i = 0 ; i < m_NumberOfMatrices; ++i)
272- {
273- this ->DestroyMatrix (i);
274- }
275- for (unsigned int i = 0 ; i < m_NumberOfVectors; ++i)
276- {
277- this ->DestroyVector (i);
278- }
279- for (unsigned int i = 0 ; i < m_NumberOfSolutions; ++i)
280- {
281- this ->DestroySolution (i);
282- }
283-
284- delete m_Matrices;
285- delete m_Vectors;
286- delete m_Solutions;
287- }
288-
289256} // namespace itk::fem
0 commit comments