Skip to content

release png,jpeg resources in destructor#22250

Merged
opencv-pushbot merged 1 commit intoopencv:4.xfrom
ocpalo:codec_fix
Aug 5, 2022
Merged

release png,jpeg resources in destructor#22250
opencv-pushbot merged 1 commit intoopencv:4.xfrom
ocpalo:codec_fix

Conversation

@ocpalo
Copy link
Copy Markdown
Collaborator

@ocpalo ocpalo commented Jul 14, 2022

Releasing resources should be done in the destructor. The user might call this method more than once.

auto res = decoder.readData(mat);
...
res = decoder.readData(mat);

This is not efficient but it is possible. In this case, behavior is undefined because of the close() call.

@alalek
Copy link
Copy Markdown
Member

alalek commented Jul 14, 2022

There should be separation of roles to avoid creation of GOD classes with unclear API:

  • decoder instance for exact input stream data. New data stream - new instance. No need to support multiple read calls/etc (because nobody would test these cases).
  • decoder as format decoder. With some decoding settings/properties.
  • decoder factory/registry.

@vpisarev
Copy link
Copy Markdown
Contributor

vpisarev commented Jul 16, 2022

@alalek, I think, removal close() from ReadData(), so that it's only called in destructor or in 'open()' does not relate to GOD concept, as you call it. close() will be called in any case.

@alalek
Copy link
Copy Markdown
Member

alalek commented Jul 16, 2022

The user might call this method more than once.

The GOD concept is about this.
Current code is not designed for that. Also there are no tests, so we can't check this proposal anyway (especially besides of JPEG/PNG codecs).

Copy link
Copy Markdown
Contributor

@asmorkalov asmorkalov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@asmorkalov
Copy link
Copy Markdown
Contributor

@ocpalo Please pay attention that Decoder classes are not designed to call methods multiple times and it;s not tested.

@ocpalo
Copy link
Copy Markdown
Collaborator Author

ocpalo commented Jul 28, 2022

@asmorkalov I see. Yet this calls are still unnecessary. Consider readHeader method fails. result will be false and readHeader method will call close() method. Since codecs are also RAII classes, destructor of this codecs calls close() method too! If i am not missing any other point, this function calls is redundant.

@asmorkalov asmorkalov self-assigned this Aug 2, 2022
@opencv-pushbot opencv-pushbot merged commit 4c1e064 into opencv:4.x Aug 5, 2022
@ocpalo ocpalo deleted the codec_fix branch August 6, 2022 12:41
@alalek alalek mentioned this pull request Aug 21, 2022
@asmorkalov asmorkalov added this to the 4.7.0 milestone Jan 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants