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

RAII #897

Merged
merged 11 commits into from
May 11, 2017
Merged

RAII #897

merged 11 commits into from
May 11, 2017

Conversation

rfschtkt
Copy link
Contributor

@rfschtkt rfschtkt commented May 9, 2017

Use the RAII idiom, e.g., to help prevent memory leaks.

@stweil
Copy link
Member

stweil commented May 9, 2017

@rfschtkt
Copy link
Contributor Author

rfschtkt commented May 9, 2017

Yes, I think it's a bug in that g++ version (4.8.4). I had successfully built with 5.4.0. I'll try again with an explicit move constructor...

@Shreeshrii
Copy link
Collaborator

Shreeshrii commented May 9, 2017 via email

@rfschtkt
Copy link
Contributor Author

rfschtkt commented May 9, 2017

Are they all tested? I think that it was valid C++11, though.

Amended, pushed...

And again...

@amitdo
Copy link
Collaborator

amitdo commented May 9, 2017

Since you change the API, I think we need Ray's approval for this PR.

@rfschtkt
Copy link
Contributor Author

rfschtkt commented May 9, 2017

If that's public API and cast in stone, maybe there should be two versions, with the backward-compatible version calling the safe version with release(). Or the leak could be fixed ad hoc, but that's not really progress.

(Added) Is the public API not tested?

(Added) I don't see any documentation mentioning ccmain/resultiterator.h as part of the API? Ah, apparently the result of tesseract::TessBaseAPI::GetIterator (), that sounds very API-ish.

@amitdo
Copy link
Collaborator

amitdo commented May 9, 2017

I would start with fixing the leak with the current API.
If you want to do it, open a new PR.

Fixing a leak is still progress...

@rfschtkt
Copy link
Contributor Author

rfschtkt commented May 9, 2017

Maybe tomorrow, but I would still suggest to add a new version and deprecate the current one, because this is C++, and RAII is one of C++'s most useful idioms (to put it mildly). That's why I made a new thread called RAII, not lookifoundaleak...

@egorpugin
Copy link
Contributor

egorpugin commented May 9, 2017

Oh, btw.
Isn't GetUTF8Text() should return C++-ish std::string? Instead of that stupid non-owning char* memory ptr.
It would be way better fix for all of this.

@rfschtkt
Copy link
Contributor Author

rfschtkt commented May 9, 2017

Yes, but it can also return nullptr, and I hadn't yet figured out whether there's a difference with the empty string. Adding to a STRING is apparently the same for nullptr and empty string, but maybe not other applications. And there's at least one use of release() that would then instead have to make a new copy.

(Correction) I meant strdup() instead of strcpy(), but that's even more expensive because it needs two passes, so let's stick with "make a new copy".

(Added) virtual ResultIterator::GetUTF8Text() hides non-virtual LTRResultIterator::GetUTF8Text, that doesn't look great...

@rfschtkt
Copy link
Contributor Author

rfschtkt commented May 10, 2017

Amended without API change, checks successful.

@amitdo
Copy link
Collaborator

amitdo commented May 10, 2017

LGTM. @stweil?

@stweil
Copy link
Member

stweil commented May 10, 2017

I plan to run a test with Valgrind later (~ 1 day) and will add my review comment then.

@amitdo
Copy link
Collaborator

amitdo commented May 10, 2017

but I would still suggest to add a new version and deprecate the current one, because this is C++, and RAII is one of C++'s most useful idioms (to put it mildly).

The new version of this PR uses RAII technique, without an API change :)
http://stackoverflow.com/questions/29372976/is-stdunique-ptr-an-application-of-raii

@rfschtkt
Copy link
Contributor Author

There seems to be another leak in void POLY_BLOCK::fill(ScrollView* window, ScrollView::Color colour), which doesn't do delete segments;. But I've had enough fun for now...

@amitdo
Copy link
Collaborator

amitdo commented May 10, 2017

I've had enough fun for now...

😆

@rfschtkt
Copy link
Contributor Author

Well, it seemed to be leaked, from comparing with other uses, although I'm not 100% certain...

Copy link
Member

@stweil stweil left a comment

Choose a reason for hiding this comment

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

Valgrind test passed (no change of alloc / free for OCR of simple image).

@rfschtkt
Copy link
Contributor Author

rfschtkt commented May 11, 2017

Rebased.

@zdenop zdenop merged commit 9b998a7 into tesseract-ocr:master May 11, 2017
@amitdo amitdo added the RAII label Mar 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants