Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fix warning C4722#2391

Merged
ahojnnes merged 6 commits into
colmap:maincolmap/colmap:mainfrom
whuaegeanse:fix_warningwhuaegeanse/colmap:fix_warningCopy head branch name to clipboard
Feb 5, 2024
Merged

Fix warning C4722#2391
ahojnnes merged 6 commits into
colmap:maincolmap/colmap:mainfrom
whuaegeanse:fix_warningwhuaegeanse/colmap:fix_warningCopy head branch name to clipboard

Conversation

@whuaegeanse

Copy link
Copy Markdown
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

ahojnnes commented Feb 2, 2024

Copy link
Copy Markdown
Contributor

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

@sarlinpe

sarlinpe commented Feb 2, 2024

Copy link
Copy Markdown
Member

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

ahojnnes commented Feb 3, 2024

Copy link
Copy Markdown
Contributor

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

sarlinpe commented Feb 3, 2024

Copy link
Copy Markdown
Member

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

ahojnnes commented Feb 3, 2024

Copy link
Copy Markdown
Contributor

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

ahojnnes commented Feb 3, 2024

Copy link
Copy Markdown
Contributor

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

@sarlinpe

sarlinpe commented Feb 5, 2024

Copy link
Copy Markdown
Member

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

@sarlinpe

sarlinpe commented Feb 5, 2024

Copy link
Copy Markdown
Member

@ahojnnes @whuaegeanse Done, PTAL.

@ahojnnes ahojnnes left a comment

Copy link
Copy Markdown
Contributor

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

Morty Proxy This is a proxified and sanitized view of the page, visit original site.