views:

117

answers:

1

I have some experience with ANTLR 2's C++ target, but have been hesitant to spend much time on ANTLR 3 because of my fears about exception safety.

Sadly, ANTLR 3 only has a C target which produces C which is "C++ compatible." This does not seem to include C++ exception safety, based on the following:

You can probably use [exceptions] carefully, but as you point out, you have to be careful with memory. The runtime tracks all its normal memory allocations so as long as you close the 'classes' correctly you should generally be OK. However, you should make sure that throwing exceptions does not bypass the normal rule clean up, such as resetting error and backtracking flags and so on.

(ANTLR-interest, circa 2009)

Does anyone have any experience using the ANTLR C target with (advanced) C++? Is it possible to safely throw exceptions? What extra code (if any) do I have to write to make it safe?

A: 

I don't have any ANTLR experience (sadly...), but there is NO way to make C code work with exceptions around. I refer you to More Effective C++, item 9 : "Use destructors to prevent resource leaks"

The idea is that if an exception is thrown during cleanup, you have no information on what is already delete()ed an what is not, and your software will leak memory. If you use auto_ptr/scroped_ptr, ou don't have to worry about this, since the compiler will deal with it itself.

But this idiom is C++-only, C was not designed with exceptions in mind.

Calvin1602