简体   繁体   English

捕获va_list错误

[英]catching va_list error

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; 每次我发送参考值参数时,都会收到“在Editor.exe中发生类型为'System.AccessViolationException'的未处理的异常”错误消息; 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. 这是完全可以理解的,但是我想知道如何将其封装在可能的try catch语句中,以防止错误关闭整个程序。 (the above does not work) (以上无效)

I'm currently calling a self-made c++ dll (with the above function) from my c# editor. 我目前正在从我的C#编辑器中调用一个自制的c ++ dll(具有上述功能)。

Here's the c# code: 这是c#代码:

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. 根据C ++标准,使用va_start传递参考args是C ++中未定义的行为。 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. 在Windows上,您应该可以使用_ try / _except捕获异常。 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 但请注意,这是未定义的行为-即使它在此版本的编译器中有效,在下一个版本中,va_start可能会决定执行完全不同的操作,这可能会导致其他错误并且不会导致__try/__except

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM