Skip to content

Commit

Permalink
WIP: Fix nanobind memory leaks
Browse files Browse the repository at this point in the history
Need to add destructor for memory allocated with new in the wrap function
  • Loading branch information
cookpa committed Nov 13, 2024
1 parent 637912b commit 142af47
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/antsImageToImageMetric.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ class ANTsImageToImageMetric {

};



template <typename MetricType>
void capsuleDestructor(void* ptr) noexcept {
auto* metricPtr = static_cast<typename MetricType::Pointer*>(ptr);
delete metricPtr;
}

template <typename MetricType>
ANTsImageToImageMetric<MetricType> wrap_metric( const typename MetricType::Pointer & itkMetric )
Expand All @@ -67,7 +70,7 @@ ANTsImageToImageMetric<MetricType> wrap_metric( const typename MetricType::Point
antsMetric.dimension = MetricType::FixedImageDimension;
antsMetric.metrictype = itkMetric->GetNameOfClass();
antsMetric.isVector = 0;
antsMetric.pointer = nb::capsule(ptr);
antsMetric.pointer = nb::capsule(ptr, capsuleDestructor<MetricType>);

return antsMetric;
}
Expand Down

0 comments on commit 142af47

Please sign in to comment.