@@ -1259,10 +1259,12 @@ _dump_metadata_to_stream(MetaDataDictionary & thisDic, const std::string & key,
12591259void
12601260NrrdImageIO::Write (const void * buffer)
12611261{
1262- Nrrd * nrrd = nrrdNew ();
1263- NrrdIoState * nio = nrrdIoStateNew ();
1264- int kind[NRRD_DIM_MAX ]{};
1265- size_t size[NRRD_DIM_MAX ]{};
1262+ const std::unique_ptr<Nrrd, decltype (&nrrdNix)> nrrdGuard (nrrdNew (), nrrdNix);
1263+ const std::unique_ptr<NrrdIoState, decltype (&nrrdIoStateNix)> nioGuard (nrrdIoStateNew (), nrrdIoStateNix);
1264+ Nrrd * nrrd = nrrdGuard.get ();
1265+ NrrdIoState * nio = nioGuard.get ();
1266+ int kind[NRRD_DIM_MAX ]{};
1267+ size_t size[NRRD_DIM_MAX ]{};
12661268
12671269 double spaceDir[NRRD_DIM_MAX ][NRRD_SPACE_DIM_MAX ];
12681270 std::fill (&spaceDir[0 ][0 ], &spaceDir[0 ][0 ] + NRRD_DIM_MAX * NRRD_SPACE_DIM_MAX , AIR_NAN );
@@ -1360,8 +1362,8 @@ NrrdImageIO::Write(const void * buffer)
13601362 : nrrdSpaceDimensionSet (nrrd, spaceDim)) ||
13611363 nrrdSpaceOriginSet (nrrd, originStd.data ()))
13621364 {
1363- char * err = biffGetDone (NRRD ); // would be nice to free(err)
1364- itkExceptionMacro (" Write: Error wrapping nrrd for " << this ->GetFileName () << " :\n " << err);
1365+ const std::unique_ptr< char , decltype (&free)> err ( biffGetDone (NRRD ), free);
1366+ itkExceptionMacro (" Write: Error wrapping nrrd for " << this ->GetFileName () << " :\n " << err. get () );
13651367 }
13661368 nrrdAxisInfoSet_nva (nrrd, nrrdAxisInfoKind, kind);
13671369 nrrdAxisInfoSet_nva (nrrd, nrrdAxisInfoSpaceDirection, spaceDir);
@@ -1447,13 +1449,9 @@ NrrdImageIO::Write(const void * buffer)
14471449 // Write the nrrd to file.
14481450 if (nrrdSave (this ->GetFileName (), nrrd, nio))
14491451 {
1450- char * err = biffGetDone (NRRD ); // would be nice to free(err)
1451- itkExceptionMacro (" Write: Error writing " << this ->GetFileName () << " :\n " << err);
1452+ const std::unique_ptr< char , decltype (&free)> err ( biffGetDone (NRRD ), free);
1453+ itkExceptionMacro (" Write: Error writing " << this ->GetFileName () << " :\n " << err. get () );
14521454 }
1453-
1454- // Free the nrrd struct but don't touch nrrd->data
1455- nrrdNix (nrrd);
1456- nrrdIoStateNix (nio);
14571455}
14581456
14591457} // end namespace itk
0 commit comments