Commit a9aa9aee authored by Quentin Khan's avatar Quentin Khan

FFmaGenericLoader: fix memory leak in writeDistributionOfParticlesFromOctree

The `particle` pointer was allocated in the forEachLeaf callback but
freed outside. If there was more than one leaf, the pointer would not be
freed before the new allocation.
parent e98aba3b
...@@ -809,38 +809,38 @@ public: ...@@ -809,38 +809,38 @@ public:
* Fwriter.writeDistributionOfParticlesFromOctree(&tree, nbParticles); * Fwriter.writeDistributionOfParticlesFromOctree(&tree, nbParticles);
* \endcode * \endcode
*/ */
template <class Toctree> template <class Toctree>
void writeDistributionOfParticlesFromOctree( Toctree *tree, const FSize N){ void writeDistributionOfParticlesFromOctree( Toctree *tree, const FSize N){
// //
this->writeHeader(tree->getBoxCenter(),tree->getBoxWidth(), N, sizeof(FReal), 4 ); this->writeHeader(tree->getBoxCenter(),tree->getBoxWidth(), N, sizeof(FReal), 4 );
FReal * particles ; tree->forEachLeaf([this]( typename Toctree::LeafClassType* leaf){
FSize nbMaxParticlesInLeaf = 0 ; FReal * particles = nullptr;
tree->forEachLeaf([&]( typename Toctree::LeafClassType* leaf){ FSize nbMaxParticlesInLeaf = 0 ;
// //
// Input // Input
const FReal*const posX = leaf->getTargets()->getPositions()[0]; const FReal*const posX = leaf->getTargets()->getPositions()[0];
const FReal*const posY = leaf->getTargets()->getPositions()[1]; const FReal*const posY = leaf->getTargets()->getPositions()[1];
const FReal*const posZ = leaf->getTargets()->getPositions()[2]; const FReal*const posZ = leaf->getTargets()->getPositions()[2];
const FReal*const physicalValues = leaf->getTargets()->getPhysicalValues(); const FReal*const physicalValues = leaf->getTargets()->getPhysicalValues();
// //
const FSize nbParticlesInLeaf = leaf->getTargets()->getNbParticles(); const FSize nbParticlesInLeaf = leaf->getTargets()->getNbParticles();
if (nbParticlesInLeaf > nbMaxParticlesInLeaf ) { if (nbParticlesInLeaf > nbMaxParticlesInLeaf ) {
nbMaxParticlesInLeaf = nbParticlesInLeaf ; nbMaxParticlesInLeaf = nbParticlesInLeaf ;
particles = new FReal[4*nbMaxParticlesInLeaf] ; particles = new FReal[4*nbMaxParticlesInLeaf] ;
} }
// //
for(FSize idxPart = 0 , j = 0; idxPart < nbParticlesInLeaf ; ++idxPart, j+=4){ for(FSize idxPart = 0 , j = 0; idxPart < nbParticlesInLeaf ; ++idxPart, j+=4){
particles[j] = posX[idxPart] ; particles[j] = posX[idxPart] ;
particles[j+1] = posY[idxPart] ; particles[j+1] = posY[idxPart] ;
particles[j+2] = posZ[idxPart] ; particles[j+2] = posZ[idxPart] ;
particles[j+3] = physicalValues[idxPart] ; particles[j+3] = physicalValues[idxPart] ;
} }
this-> writeArrayOfReal(particles, 4, nbParticlesInLeaf) ; this->writeArrayOfReal(particles, 4, nbParticlesInLeaf) ;
}); delete [] particles ;
delete [] particles ; });
} }
private: private:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment