Skip to content

Commit e24f142

Browse files
committed
avoid double-free when C++-wrapper ColorProfile_nclx is copied (#1641)
1 parent b117d53 commit e24f142

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

libheif/api/libheif/heif_cxx.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ namespace heif {
283283
public:
284284
ColorProfile_nclx();
285285

286-
~ColorProfile_nclx();
286+
~ColorProfile_nclx() = default;
287287

288288
heif_color_primaries get_color_primaries() const;
289289

@@ -305,10 +305,9 @@ namespace heif {
305305
void set_full_range_flag(bool is_full_range);
306306

307307
private:
308-
ColorProfile_nclx(heif_color_profile_nclx* nclx)
309-
{ mProfile = nclx; }
308+
ColorProfile_nclx(heif_color_profile_nclx* nclx);
310309

311-
heif_color_profile_nclx* mProfile;
310+
std::shared_ptr<heif_color_profile_nclx> mProfile;
312311

313312
friend class Image;
314313
};
@@ -813,12 +812,15 @@ namespace heif {
813812

814813
inline ColorProfile_nclx::ColorProfile_nclx()
815814
{
816-
mProfile = heif_nclx_color_profile_alloc();
815+
auto profile = heif_nclx_color_profile_alloc();
816+
mProfile = std::shared_ptr<heif_color_profile_nclx>(profile,
817+
[](heif_color_profile_nclx* p) { heif_nclx_color_profile_free(p); });
817818
}
818819

819-
inline ColorProfile_nclx::~ColorProfile_nclx()
820+
inline ColorProfile_nclx::ColorProfile_nclx(heif_color_profile_nclx* nclx)
820821
{
821-
heif_nclx_color_profile_free(mProfile);
822+
mProfile = std::shared_ptr<heif_color_profile_nclx>(nclx,
823+
[](heif_color_profile_nclx* p) { heif_nclx_color_profile_free(p); });
822824
}
823825

824826
inline heif_color_primaries ColorProfile_nclx::get_color_primaries() const
@@ -938,7 +940,7 @@ namespace heif {
938940

939941
inline void Image::set_nclx_color_profile(const ColorProfile_nclx& nclx)
940942
{
941-
Error err = Error(heif_image_set_nclx_color_profile(m_image.get(), nclx.mProfile));
943+
Error err = Error(heif_image_set_nclx_color_profile(m_image.get(), nclx.mProfile.get()));
942944
if (err) {
943945
throw err;
944946
}

0 commit comments

Comments
 (0)