Added a copy constructor and an overloaded assignment operator to FileStorage#17458
Added a copy constructor and an overloaded assignment operator to FileStorage#17458EricFlorin wants to merge 5 commits intoopencv:masterfrom EricFlorin:FileStorage_Assignment_Operator
Conversation
…EricFlorin/opencv into FileStorage_Assignment_Operator_Fix
|
@EricFlorin Thanks for the patch! Please take a look on CI status, buildbot reports compiler warnings: |
|
Could you also add simple test for the new functions to ensure stability in future. |
|
Hey @asmorkalov! I wanted to say thanks for the review! I also want to ask about what kinds of tests you may be looking for when it comes to the copy and assignment operators for |
|
XML/JSON/YAML formats handling is done by different |
|
@EricFlorin Please take a look on CI status: https://pullrequest.opencv.org/buildbot/builders/precommit_linux64/builds/26297/steps/compile%20release/logs/warnings%20%282%29. CI bot reports build warnings: |
…d added new checks to copy constructor and assignment operator
|
Hey @asmorkalov, I got around to fixing the warnings on my side (we will see what the CI bot says). I also modified the reproducer code to act as a test, as well as create my own test script to test different types of cases. The scripts and support files are in this repository that I created.
If I am missing a case in my custom test script, please let me know and I will add it! |
alalek
left a comment
There was a problem hiding this comment.
Thank you for the contribution!
Please avoid implementing of "deep-copy" logic for FileStorage.
Could you please add simple tests (including from the original issue) to test_io.cpp?
| /** @brief Copy constructor for FileStorage | ||
|
|
||
| Copy constructor that creates deep copies of passed in cv::FileStorage objects. Based off the full | ||
| constructor. |
There was a problem hiding this comment.
Almost all non-trivial OpenCV classes are designed as a smart pointers (this behavior is aligned with other language bindings):
- assignment operation means increasing object lifetime (through refcounter)
- optional deep-copy is through
.clone()operations
Deep copy is over-complicated thing for FileStorage (especially in case of write mode), so let avoid that completely.
|
@EricFlorin Do you have chance to implement proposal from @alalek |
|
Hey @asmorkalov and @alalek, Sorry for the long silence this week as I was having final exams. I did look at the proposal made by @alalek and I did roll back the deep-copy implementation on my end. However, I am stuck on coming up with a solution using smart pointers. Here's what I found so far using the reproducer code given in #17412 :
From there, I am not sure what should I do. I was thinking of having the Any thoughts? |
|
@EricFlorin, thank you for the detailed research. I fixed the problem in #17841. The solution was simple – just remove "p.release()" from FileStorage destructor, because otherwise the smart pointer was released twice |
|
Thanks for letting me know @vpisarev! I was wondering what the error could be. |
tl:dr -> Fixes #17412
Hello everyone,
This is going to be my first pull request, so I'm sorry if I missed something in my submission. I appreciate any feedback as this is my first time contributing to an open source project.
Quick summary of issue #17412: a
cv::FileStorageobject that was assigned by anothercv::FileStorageobject was unusable as it was causing segfaults every time an access occurs.I look over the issue replication code provided in #17412, and I noticed that
fswas being assigned to a temporarycv::FileStorageobject. I went through the execution path of the assignment using my debugger, and noticed thatfswas not being assigned a deep copy of the temporarycv::FileStorageobject.Since
fswas not given a deep copy of the temporarycv::FileStorageobject, it loses access to the file reference in the temporarycv::FileStorageobject after the temporary object is destroyed upon assignment completion.I fixed the issue by adding a copy constructor and an overloaded assignment operator to
cv::FileStoragethat creates deep copies ofcv::FileStorageobjects. I feel that there is probably a better way of fixing this issue, but this is what I came up with for now.Thank you for reading!
Eric
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.