8000 Fix warning C4722 by whuaegeanse · Pull Request #2391 · colmap/colmap · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix warning C4722 #2391

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

Merged
merged 6 commits into from
Feb 5, 2024
Merged

Fix warning C4722 #2391

merged 6 commits into from
Feb 5, 2024

Conversation

whuaegeanse
Copy link
Contributor

Vistual Studio 2022 outputs compiler warning C4722.

warning C4722: 'colmap::LogMessageFatalThrow<std::invalid_argument>::~LogMessageFatalThrow<std::invalid_argument>': destructor never returns, potential memory leak

This PR attempts to suppress the C4722 warning. But I'm not sure if this is a good idea.

Compiler Warning (level 1) C4722

'function' : destructor never returns, potential memory leak

The flow of control terminates in a destructor. The thread or the entire program will terminate and allocated resources may not be released. Furthermore, if a destructor will be called for stack unwinding during exception processing, the behavior of executable is undefined.

To resolve, remove the function call that causes the destructor to not return.

@ahojnnes
Copy link
Contributor
ahojnnes commented Feb 2, 2024

Throwing from a destructor is indeed not a good idea and will leak memory. @sarlinpe FYI

@sarlinpe
Copy link
Member
sarlinpe commented Feb 2, 2024

As mentioned in #2376, I checked with valgrind (on Unix) that this doesn't incur any memory leak. I agree that in general throwing from a destructor is bad design, but I think that it's fine for this case - unless the behavior is different for Unix vs Windows?

The only scenario in which this could bite is the following, which causes an std::terminate:

std::string PrintingFn(...) {
    if (...) {
        LOG(FATAL_THROW) << "Error in PrintingFn";
    }
    return ...;
}

LOG(FATAL_THROW) << "Error: " << PrintingFn(...);

We don't have any such example in the codebase and it's easy to avoid. Besides, the throwing destructor is the only design (AFAIK) that allows the nice << syntax.

@ahojnnes
Copy link
Contributor
ahojnnes commented Feb 3, 2024

I assume that address sanitizer (which is already executed as part of CI) should also catch these. Maybe we should have a few unit tests that exercise the new exception macros explicitly, so they get executed in our CI pipeline.

I am not sure I fully understand why the above example would cause a memory leak or std::terminate. I would have to dig into glog's code I guess?

@sarlinpe
Copy link
Member
sarlinpe commented Feb 3, 2024

Having tests covering the macro is a good idea, I can give it a try unless @whuaegeanse you have time.

Regarding the above example: PrintingFn throws an exception, the stack is unwound, LogMessageFatalThrow of the calling scope gets destructed, which throws a second exception. As the warning suggests, this has undefined behavior - on Unix this causes an std::terminate.

@ahojnnes
Copy link
Contributor
ahojnnes commented Feb 3, 2024

OK, got it, thanks for the explanation. I guess we could detect such cases (where our log exceptions are thrown recursively) and then not throw a second time, crash the process, or log out both. This could be done with an atomic boolean flag or so in the LogFatalThrowLogMessage class.

@ahojnnes
Copy link
Contributor
ahojnnes commented Feb 3, 2024

Not sure if there is another way to prevent the problem in the first place... Probably there is but requires time to dig.

@sarlinpe
Copy link
Member
sarlinpe commented Feb 5, 2024

We can detect such cases by checking std::uncaught_exception. I'll give it a try.

@sarlinpe
Copy link
Member
sarlinpe commented Feb 5, 2024

@ahojnnes @whuaegeanse Done, PTAL.

Copy link
Contributor
@ahojnnes ahojnnes left a comment

Choose a reason for hiding this comment

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

Thanks, looks great to me, especially now coupled with the tests :-)

@ahojnnes ahojnnes enabled auto-merge (squash) February 5, 2024 19:40
@ahojnnes ahojnnes merged commit 9e7ab80 into colmap:main Feb 5, 2024
@whuaegeanse whuaegeanse deleted the fix_warning branch February 7, 2024 04:33
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.

3 participants
0