Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove const from outbuf #202

Merged
merged 1 commit into from
Feb 20, 2024
Merged

Remove const from outbuf #202

merged 1 commit into from
Feb 20, 2024

Conversation

BradleyMarie
Copy link
Contributor

The const marking on outbuf/buffer means that the buffer allocated by SaveEXRToMemory cannot be passed to free without a compiler error.

For example, the following code will not compile because SaveEXRToMemory requires buffer to be const.

bool Write(const std::vector<float>& image, std::pair<int, int> size,
           std::ostream& output) {
  const unsigned char* buffer;  // Required to be const by signature of SaveEXRToMemory
  int bytes_written =
      SaveEXRToMemory(image.data(), /*width=*/size.second,
                      /*height=*/size.first, /*components=*/3,
                      /*save_as_fp16=*/1, /*buffer=*/&buffer, /*err=*/nullptr);

  if (bytes_written < 0) {
    return false;
  }

  output.write(reinterpret_cast<const char*>(buffer),
               static_cast<std::streamsize>(bytes_written));

  free(buffer);  // This line triggers a compile error

  return true;
}

Unfortunately, this is a breaking API change but since it wasn't possible to call free on the allocated buffer without either a compiler error or a const-cast, I believe this break is a necessary evil.

@syoyo syoyo merged commit 6c8742c into syoyo:release Feb 20, 2024
5 checks passed
@syoyo
Copy link
Owner

syoyo commented Feb 20, 2024

Good find! Thanks!

breaking API change

This should be OK. There was no use of SaveEXRToMemory in TinyEXR examples/tests so couldn't find the const issue. Probably no one got success to use SaveEXRToMemory...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants