I’m currently using the below function to print my debug strings:
void Script::OutputDebugStringN(const char *format, ...)
{
char outstring[256];
memset(outstring, 0, sizeof(outstring));
try
{
va_list args = {0};
va_start(args, format); //args = (va_list) (&format+1);
vsprintf(outstring, format, args);
va_end(args);
OutputDebugString(outstring);
}
catch (...) //most likely reference val arg error (va_list doesn't support ref args)
{
OutputDebugString("[OutputDebugStringN] Something went wrong\n");
}
}
I receive a “An unhandled exception of type ‘System.AccessViolationException’ occurred in Editor.exe” error message each time I send in a reference value argument; which is completely understandable, but I’m wondering how I could encapsulate this in a possible try catch statement to prevent the error from shutting down my entire program. (the above does not work)
I’m currently calling a self-made c++ dll (with the above function) from my c# editor.
Here’s the c# code:
private void ComponentDispatcher_ThreadIdle(object sender, EventArgs e)
{
//render display window (if something is not blocking it)
if (renderViewHost.Update)
{
try { NativeMethods.UpdateRenderWindow(); }
catch (Exception exc)
{ Debug.WriteLine("[ThreadIdle::UpdateRenderWindow] Exception caught: {0}" + exc); }
}
}
I just find it unacceptable that my function to print out my debug strings has in itself an error. Any ideas? All comments on bad code/observation are welcomed.
Passing reference args with va_start is undefined behaviour in C++ as per the C++ standard. That means you cannot predict what the compiler will do. Even if something works in this compiler, it may not work on an another compiler. It may not even work on the next version of the same compiler.
That said, there may be platform specific constructs which would help you cause the exception. For eg. on windows, you should be able to use the _try/_except to catch the exception. But be warned that this is undefined behaviour – even if it works in this version of the compiler, in the next version, va_start may decide to do something totally different which may result in other errors & not a crash which can be caught in
__try/__except