8

I want to use std::unique_ptr in combination with FreeImage's FITAG. The code in plain C would be:

... load image;

FITAG* tag = NULL;
FreeImage_GetMetadata(FIMD_EXIF_EXIF, bitmap, "Property", &tag);
... do some stuff with tag;
FreeImage_DeleteTag(tag);

... delete image;

My attempt with unique_ptr:

std::unique_ptr<FITAG, void(*)(FITAG*)> tag(NULL, &FreeImage_DeleteTag);
FreeImage_GetMetadata(FIMD_EXIF_EXIF, bitmap, "Property", &tag.get());

which obviously returns:

cannot take the address of an rvalue of type 'pointer' (aka 'FITAG *')

How would I solve this?

Thanks a lot in advance.

2
  • 1
    No, FreeImage_GetMetadata expects a FITAG**. Commented Jul 3, 2014 at 0:23
  • 1
    If FreeImage_DeleteTag doesn't have a signature which unique_ptr will accept, then write a wrapper function which does have a necessary signature and which then calls FreeImage_DeleteTag appropriately. Commented Jul 3, 2014 at 0:26

2 Answers 2

11

Reverse the order of operations; first acquire the resource and then construct the unique_ptr.

FITAG *p = NULL;
FreeImage_GetMetadata(FIMD_EXIF_EXIF, bitmap, "Property", &p);
std::unique_ptr<FITAG, void(*)(FITAG*)> tag(p, &FreeImage_DeleteTag);
Sign up to request clarification or add additional context in comments.

Comments

3
tag.get()

This returns an r-value. You can't get the address of an r-value( temporary ). Your best bet is to do this instead:

auto ptr = tag.get();
FreeImage_GetMetadata(FIMD_EXIF_EXIF, bitmap, "Property", &ptr);

This will allow you to pass a FITAG** into the function. If you meant to pass a FITAG* into the function, just remove the & since tag.get() returns the pointer, not the value.

FreeImage_GetMetadata(FIMD_EXIF_EXIF, bitmap, "Property", tag.get());

1 Comment

Your first example will compile, but the unique_ptr will no longer own the resource allocated by FreeImage_GetMetadata.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.