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

Replace ASSERT with CORRADE_ASSERT in PTexMeshData.cpp #225

Merged
merged 10 commits into from
Sep 24, 2019
Merged

Conversation

bigbike
Copy link
Contributor

@bigbike bigbike commented Sep 19, 2019

If you look into the implementation of ASSERT, you will find that it actually cannot output any error message when the condition is failed.

Apply the CORRADE_ASSERT in the Corrade library instead.

In this PR:
-) replaced all the ASSERT with CORRADE_ASSERT;
-) added "Error: " to some of the error messages;

Motivation and Context

How Has This Been Tested

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have completed my CLA (see CONTRIBUTING)
  • I have added tests to cover my changes.
  • All new and existing tests passed.

If you look into the implementation of ASSERT, you will find that it actually cannot output error message when the condition is failed.

Apply the CORRADE_ASSERT shipped with Corrade library instead.

In this PR:
-) replaced all the ASSERT with CORRADE_ASSERT;
-) added "Error: " to some of the error messages;
@facebook-github-bot facebook-github-bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Sep 19, 2019
@bigbike
Copy link
Contributor Author

bigbike commented Sep 19, 2019

This PR is ready to be reviewed. Thanks.

Copy link
Collaborator

@msavva msavva left a comment

Choose a reason for hiding this comment

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

Thanks for catching this @bigbike . Let's create an issue to replace use of ASSERT with CORRADE_ASSERT globally in a separate PR.

@@ -512,7 +538,7 @@ void PTexMeshData::parsePLY(const std::string& filename,
colorOffsetBytes = offsetSoFarBytes;
offsetSoFarBytes += colorBytes;
} else {
ASSERT(false);
CORRADE_ASSERT(false, "Error: unknown.", );
Copy link
Collaborator

Choose a reason for hiding this comment

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

CORRADE_ASSERT_UNREACHABLE() would be more descriptive in this case I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good to know. will change.

"Error: number of vertices is not greater than 0", );
CORRADE_ASSERT(
positionDimensions > 0,
"Error: the dimensions of the position is not greater than 0", );
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is the Error: prefix really needed? :) In every case it's the last thing you see before it aborts.

In Magnum itself I'm instead prefixing every assert with class/function name to make it easier to figure out where it blew up. In this case it would be e.g. PTexMeshData::parsePly(): the dimensions....

(Also, I'm planning to add configurable message prefixes / coloring in the near future.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good suggestion. I used to do what you recommended as well, and then I stopped to do so since the ASSERT, which used LOG, would output the filename and line number.

@mosra: Another question to you about the CORRADE_ASSERT, it seems it would generate a run-time error instead of exiting the program (like e.g., exit(-1) does)?

Copy link
Collaborator

Choose a reason for hiding this comment

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

A bit more background on this: there's CORRADE_INTERNAL_ASSERT() which doesn't have a message but OTOH prints file/line info. That one is designed for internal assertions which shouldn't ever fire as a result of bad API usage by the library user -- and since the internal error messages would be in most cases just "internal state that's too complex to explain in a single sentence is totally messed up, sorry for your bad experience" I decided to not have them there.

OTOH I thought showing file/line info in CORRADE_ASSERT() would be mostly useless for the user as they rather need to know what function name caused everything to blow up (as opposed to knowing it's line 2765 of AbstractTexture.cpp, for example). So for those there's just a human-readable formatted message.

Basically when deciding which of them to use, I think whether the assertion can happen rather from bad usage on the API user side (in which case it's more important to show why is it wrong), or it's just an internal sanity check that shouldn't hit when just passing bad parameters to a function (in which case file/line info is better). ... Hope those design decisions still make sense now :)

it seems it would generate a run-time error instead of exiting the program (like e.g., exit(-1) does)?

Yes, it calls std::abort() instead of std::exit() -- reason is that the former generates a proper backtrace (or enters a debugger on some systems), which is what you usually want when an assert hits. The exit() would just exit without giving you any chance to know from where it was fired.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, I see your point on the design decisions. I totally understand you.

To decide which one to use, we need to think about the purpose of the error message (to expedite debugging or FYI only), and who is the audience (the developer or the user of the system).

Though it is pretty vague in some cases, it is not hard to determine.

I will go over all the places I changed, and update accordingly.

Copy link
Contributor Author

@bigbike bigbike Sep 19, 2019

Choose a reason for hiding this comment

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

Oh, you used std::abort(). That is why.

Yes, the returned backtrace is rather useful sometimes for the developer.

But again, if the audience is a user of our simulator, in my humble opinion, it is more comfortable for her to see the program is terminated normally (exit()) instead of abnormally (abort()).

Copy link
Contributor Author

@bigbike bigbike Sep 19, 2019

Choose a reason for hiding this comment

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

But it didn't terminate normally -- It terminated due to a failed assertion!

Yes, indeed, that is actually what happened. I agree, but that is just from the developer's perspective.

But from a user's view, maybe it is more comfortable (or friendly) if the system can "pretend" to quit "normally",meaning it exits without a run-time error (otherwise, there would be an annoying pop-up dialog-box on mac e.g.,). Just my opinion.

Copy link
Contributor

@erikwijmans erikwijmans Sep 20, 2019

Choose a reason for hiding this comment

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

I definitely don't think people want things to crash with no indication that anything went wrong. It seems very hard for them to figure out that something is broken if we don't inform them in a fairly loud way.

Copy link
Collaborator

Choose a reason for hiding this comment

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

If you want to exit() with a message, use Fatal{} << blah;. It exits the "normal" way, it's what Utility::Arguments do when you pass a wrong thing on the command line. Asserts are loud by definition and yes, are meant for developers. If a non-developer hits an assert, it's the app developers fault this wasn't checked and handled gracefully.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmmm. Maybe something like FATAL_IF(<blah>, <msg>) would be useful? Some of the stuff here is checking if a file exists, so a user could in theory hit that. The assert style synatx is nice than if (<blah>) Fatal{} << <msg>; IMO

Copy link
Collaborator

Choose a reason for hiding this comment

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

As I understand it, asserts are macros mainly so there's a possibility to compile them out in optimized builds. Apart from that I'm trying to avoid macros as much as possible, so the non-macroy if() is no problem for me.

-) replaced "Error" with class::functionName at the beginning of every error message;
-) used CORRADE_ASSERT_UNREACHABLE;
@bigbike
Copy link
Contributor Author

bigbike commented Sep 19, 2019

Code is updated. Let me know if you have more concerns. Thanks.

@bigbike bigbike reopened this Sep 23, 2019
@bigbike
Copy link
Contributor Author

bigbike commented Sep 23, 2019

Let me know if you have more concerns before I merge it in.

ASSERT(submeshID >= 0 && submeshID < renderingBuffers_.size());
CORRADE_ASSERT(submeshID >= 0 && submeshID < renderingBuffers_.size(),
"PTexMeshData::uploadBuffersToGPU: the submesh ID "
<< submeshID << " is out of range.",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
<< submeshID << " is out of range.",
<< submeshID << "is out of range.",

Neither with Debug / Warning / Error / Fatal nor with the assert macros you need to wrap values with spaces, that's done automatically to save you extra typing. Putting the space there would make it doubly-spaced. Docs.

Same in all other cases ;)

@bigbike bigbike merged commit f433ea7 into master Sep 24, 2019
@bigbike
Copy link
Contributor Author

bigbike commented Sep 24, 2019

Thanks all reviewers.

@bigbike bigbike deleted the replica_assert branch September 24, 2019 23:13

// Parse parameters
const auto& paramsFile = atlasFolder + "/parameters.json";
ASSERT(io::exists(paramsFile));
if (io::exists(paramsFile)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a bug.

bigbike added a commit that referenced this pull request Sep 25, 2019
bigbike added a commit that referenced this pull request Sep 25, 2019
eundersander pushed a commit to eundersander/habitat-sim that referenced this pull request Aug 6, 2020
Ram81 pushed a commit to Ram81/habitat-web-sim that referenced this pull request Dec 10, 2020
…rch#225)

* Replce ASSERT with CORRADE_ASSERT in PTexMeshData.cpp

If you look into the implementation of ASSERT, you will find that it actually cannot output error message when the condition is failed.

Apply the CORRADE_ASSERT shipped with Corrade library instead.

In this PR:
-) replaced all the ASSERT with CORRADE_ASSERT;
-) added "Error: " to some of the error messages;

* minor

* address reviewer's concerns.

-) replaced "Error" with class::functionName at the beginning of every error message;
-) used CORRADE_ASSERT_UNREACHABLE;

* minor

* use Fatal{} instead of ASSERT at certain places

* remove space

* minor

* minor

* minor
Ram81 pushed a commit to Ram81/habitat-web-sim that referenced this pull request Dec 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Do not delete this pull request or issue due to inactivity.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants