Skip to content

Instead of calling exit(1), teach triangle to throw exception#1800

Merged
alecjacobson merged 1 commit intomainfrom
triexit
May 3, 2021
Merged

Instead of calling exit(1), teach triangle to throw exception#1800
alecjacobson merged 1 commit intomainfrom
triexit

Conversation

@alecjacobson
Copy link
Copy Markdown
Contributor

Switch triangle version to one where it's possible to customize the function triexit which is called when triangle fails.

Meanwhile, in libigl provide this custom implementation to throw a std::runtime_exception.

(Note, this change is unrelated to switching to https://github.com/qnzhou/trianglelite . That may be a good idea in the future but doesn't immediately solve the problem of triangle calling exit)

@jdumas
Copy link
Copy Markdown
Collaborator

jdumas commented May 2, 2021

Hmm this is a really murky modification. At this point we might as well modify our version of triangle to compile with .cpp and throw an exception directly. Providing an incomplete "triangle" lib where triexit is undefined can cause other problems down the line. E.g. what if another project have two components A and B, but A depends on libigl, and B only depends on triangle? Suddenly B is linked against triangle but doesn't provide implementation for triexit (which is in libigl). To make things more complicated libigl is currently a "header-only" library, but the implementation currently resides in a .cpp in the include/ folder, which is problematic.

@alecjacobson
Copy link
Copy Markdown
Contributor Author

alecjacobson commented May 2, 2021

Hmm this is a really murky modification. At this point we might as well modify our version of triangle to compile with .cpp and throw an exception directly. Providing an incomplete "triangle" lib where triexit is undefined can cause other problems down the line. E.g. what if another project have two components A and B, but A depends on libigl, and B only depends on triangle? Suddenly B is linked against triangle but doesn't provide implementation for triexit (which is in libigl).

Happy to accept a PR that does that change instead. This was meant to be a minimal change to libigl/triangle. I don't know what converting the whole thing to .cpp would entail (maybe that's what qnzhou/trianglelite; but it still just downloads the original triangle with this exit problem).

I'm not sure I 100% understand the A/B scenario. In this scenario, some component of the project is using triangle via libigl but not linking to libigl? Is this a scenario worth worrying about?

To make things more complicated libigl is currently a "header-only" library, but the implementation currently resides in a .cpp in the include/ folder, which is problematic.

This aspect should be fixed now.

@jdumas
Copy link
Copy Markdown
Collaborator

jdumas commented May 2, 2021

Happy to accept a PR that does that change instead. This was meant to be a minimal change to libigl/triangle. I don't know what converting the whole thing to .cpp would entail (maybe that's what qnzhou/trianglelite; but it still just downloads the original triangle with this exit problem).

Actually you don't need to convert the whole file to .cpp. Simply add a file triexit.cpp in the libigl/triangle repo instead of adding your custom triexit function into libigl. Bonus point: you get to keep the TRIANGLE_CUSTOM_TRIEXIT define private to the triangle library, and it doesn't leak into client headers.

I'm not sure I 100% understand the A/B scenario. In this scenario, some component of the project is using triangle via libigl but not linking to libigl? Is this a scenario worth worrying about?

Yes, this happens all the time. If we put aside the triangle licensing issues for the sake of the argument, in a large product codebase you can have plenty of components using triangle directly, with other parts of the code using triangle through libigl (e.g. anyone who wants to use polyfem). Putting triexit() directly in libigl instead of triangle makes the triangle not self-contained anymore, and makes integration more problematic.

It is a small change to add the file triexit.cpp directly to our libigl/triangle repo instead, so I would prefer to do that for now.

{
void triexit(int status)
{
throw std::runtime_error("triexit("+std::to_string(status)+")");
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Btw I am not sure what happens when you put extern "C" around a code block and then throw a C++ exception inside...

Copy link
Copy Markdown
Collaborator

@jdumas jdumas May 2, 2021

Choose a reason for hiding this comment

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

@jdumas
Copy link
Copy Markdown
Collaborator

jdumas commented May 2, 2021

Oh and yes qnzhou/trianglelite wouldn't solve this issue either, as it also depends on vanilla triangle. qnzhou/trianglelite is more like an alternative to the whole igl/triangle wrapper.

@jdumas
Copy link
Copy Markdown
Collaborator

jdumas commented May 2, 2021

Btw I just tried renaming triangle.c into triangle.cpp it compiles perfectly fine out of the box, so to avoid the undefined behavior of throwing exceptions across C functions, I would rather switch triangle to .cpp directly.

@alecjacobson
Copy link
Copy Markdown
Contributor Author

alecjacobson commented May 2, 2021 via email

@jdumas jdumas added this to the v3.0.0 milestone May 3, 2021
@jdumas jdumas force-pushed the triexit branch 2 times, most recently from 0c70388 to a0f9495 Compare May 3, 2021 06:29
@jdumas
Copy link
Copy Markdown
Collaborator

jdumas commented May 3, 2021

Alright PR updated -- changes are much smaller now. Feel free to merge.

@alecjacobson alecjacobson merged commit 2ce04b5 into main May 3, 2021
@alecjacobson alecjacobson deleted the triexit branch May 3, 2021 20: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.

2 participants