VS2015 output window bad_any_cast spam



I am evaluating chaiscript for a project of ours and so far it looks really promising.

There is however bit of a concern - after executing even a simple script like the one in the documentation:

class Rectangle { def Rectangle() { } } var rect = Rectangle()

while working fine, outputs there 2 lines

Exception thrown at 0x74EC4598 in chai.exe: Microsoft C++ exception: chaiscript::detail::exception::bad_any_cast at memory location 0x01448170.
Exception thrown at 0x74EC4598 in chai.exe: Microsoft C++ exception: chaiscript::exception::bad_boxed_cast at memory location 0x01448378.

49 (!!) times each in the Ouput Window of Visual Studio 2015. We are actually using the output a lot as a debug log and such spam will render it useless.

What are all these exceptions about - am I doing something wrong, or it is an expected behavior? Can anything be done about it?



Exceptions are used internally for determining the types of objects at function dispatch time. They are expected and not an error.

I’ve gone though many rounds of trying to reduce those exceptions as much as possible, but it sounds like you are seeing more than I would expect.

Please provide me with a 100% complete example that I can get into my testing suite and address these concerns.



Hey and thanks for the quick reply!

I’ve trimmed my test project down to:

#include <chaiscript/chaiscript.hpp>
#include <chaiscript/chaiscript_stdlib.hpp>
#include <chaiscript/dispatchkit/bootstrap_stl.hpp>
#include <chaiscript/dispatchkit/function_call.hpp>

int main( int argc , char * argv[] ) 
    chaiscript::ChaiScript ch( chaiscript::Std_Lib::library( ) );

    static const char script[ ] = 
class Rectangle
    def Rectangle() { }
var rect = Rectangle( );


        ch.eval( script );
    catch ( std::exception e )
        printf( " >>> Exception thrown: %s \n" , e.what( ) );

    return 1;

When compiled with Visual Studio 2015 it seems to be throwing 59 times, compiled with clang++ 3.6 it throws 81 times.

Sadly I think this would be a deal breaker for us, which is a damn shame because everything else seems to be working great. You wouldn’t happen to be working on a no-exceptions-unless-theres-an-error branch would you? :smile:



What version of ChaiScript are you using?


I guess 5.7.1 - I’ve downloaded this archive:



Ok, I’m looking into it. I do see the number of exceptions you are describing and I am surprised by them. I expect to have a real answer for you shortly.

My I ask what kind of project you are evaluating ChaiScript for?


It’s gaming related - we want to add an option to write scripts for certain events in our in-house game engine.

We are not exactly set on the details yet though.


This commit gets you specific test case down to 0 internal exceptions:

I cannot promise that you’ll never have internal exceptions - it’s simply too expensive to eliminate them altogether.

Basically we do a general filter to see if a function call can succeed. If we think it can - then we attempt to execute the function call. If the attempted call fails then we unwind and try the next potential match.

Trying to do a complete filter against every function call ends up penalizing the common cases to optimize for the un-common cases.

But I expect you to see significantly fewer exceptions and probably 0 with this change.


Thank you!

I’ve updated the library and indeed it throws 0 times. Also I’ve played a bit with it and couldn’t get it to throw at all!