c++ - 为什么这个 Haxe try-catch block 在对 C++ 目标使用 Release模式时仍然崩溃

标签 c++ haxe flashdevelop haxeflixel

我有一个 HaxeFlixel 项目,它在 Debug模式下对 misc 目标(包括 flash、neko 和 windows)工作正常。但是在 Release模式 下针对 Windows,我遇到了意外崩溃,令人惊讶的是它发生在 try-catch block 内。这是崩溃函数:

/**
 * Will safely scan a parent node's children, search for a child by name, and return it's text.
 * @param   parent an Fast object that is parent of the `nodeNamed` node
 * @param   nodeName the node's name or a comma-separated path to the child (will scan recursively)
 * @return node's text as String, or null if child is not there
 */
public static function getNodeText(parent:Fast, nodeName:String):String {

    try {
        var _node : Fast = getNodeNamed(parent, nodeName);

        //if (_node == null)
        //  return null;

        // next line will crash if _node is null
        var it :Iterator<Xml> = _node.x.iterator();
        if ( it == null || !it.hasNext() )
            return null;
        var v = it.next();
        var n = it.next();
        if( n != null ) {
            if( v.nodeType == Xml.PCData && n.nodeType == Xml.CData && StringTools.trim(v.nodeValue) == "" ) {
                var n2 = it.next();
                if( n2 == null || (n2.nodeType == Xml.PCData && StringTools.trim(n2.nodeValue) == "" && it.next() == null) )
                    return n.nodeValue;
            }
            //does not only have data (has children)
            return null;
        }
        if( v.nodeType != Xml.PCData && v.nodeType != Xml.CData )
            //does not have data";
            return null;
        return v.nodeValue;
    }catch (err:Dynamic) {
        trace("Failed parsing node Text [" + nodeName+"] " + err );
        return null;
    }
}

通过启用 if (_node == null) return null; 行,它再次安全工作。通过将错误捕获为 Dynamic,我认为我应该捕获所有可能的错误类型!为什么会这样?为什么它会出现在 Release模式中?

我的 IDE 是 Fl​​ashDevelop,我使用的是 HaxeFlixel 3.3.6、lime 0.9.7 和 openFL 1.4.0,如果这有什么不同的话

编辑:我怀疑这与翻译后的 C++ 代码如何错过 Dynamic 异常有关。等效生成的 C++ 代码是:

STATIC_HX_DEFINE_DYNAMIC_FUNC2(BaxXML_obj,_getNodeNamed,return )

::String BaxXML_obj::getNodeText( ::haxe::xml::Fast parent,::String nodeName){
    HX_STACK_FRAME("bax.utils.BaxXML","getNodeText",0x4a152f07,"bax.utils.BaxXML.getNodeText","bax/utils/BaxXML.hx",56,0xf6e2d3cc)
    HX_STACK_ARG(parent,"parent")
    HX_STACK_ARG(nodeName,"nodeName")
    HX_STACK_LINE(56)
    try
    {
    HX_STACK_CATCHABLE(Dynamic, 0);
    {
        HX_STACK_LINE(57)
        ::haxe::xml::Fast _node = ::bax::utils::BaxXML_obj::getNodeNamed(parent,nodeName);      HX_STACK_VAR(_node,"_node");
        HX_STACK_LINE(63)
        Dynamic it = _node->x->iterator();      HX_STACK_VAR(it,"it");
        // ...  Let's skip the irrelevant code
    }
    catch(Dynamic __e){
        {
            HX_STACK_BEGIN_CATCH
            Dynamic err = __e;{
                HX_STACK_LINE(82)
                ::String _g5 = ::Std_obj::string(err);      HX_STACK_VAR(_g5,"_g5");
                HX_STACK_LINE(82)
                ::String _g6 = (((HX_CSTRING("Failed parsing node Text [") + nodeName) + HX_CSTRING("] ")) + _g5);      HX_STACK_VAR(_g6,"_g6");
                HX_STACK_LINE(82)
                ::haxe::Log_obj::trace(_g6,hx::SourceInfo(HX_CSTRING("BaxXML.hx"),82,HX_CSTRING("bax.utils.BaxXML"),HX_CSTRING("getNodeText")));
                HX_STACK_LINE(83)
                return null();
            }
        }
    }
    HX_STACK_LINE(56)
    return null();
}

最佳答案

你定义了什么haxedefs?

将这些添加到您的 project.xml 可能会有所帮助:

<haxedef name="HXCPP_CHECK_POINTER"/>  <!--makes null references cause errors-->
<haxedef name="HXCPP_STACK_LINE" />    <!--if you want line numbers-->
<haxedef name="HXCPP_STACK_TRACE"/>    <!--if you want stack traces-->

您也可以试试 crashdumper 库: https://github.com/larsiusprime/crashdumper

(Crashdumper 将默认打开 HXCPP_CHECK_POINTER 作为其 include.xml 的一部分,并将为 hxcpp 的错误和 openfl/lime 的未捕获错误事件设置 Hook )

关于c++ - 为什么这个 Haxe try-catch block 在对 C++ 目标使用 Release模式时仍然崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30628025/

相关文章:

c++ - 在 if 条件下检测 "="而不是 "=="拼写错误

c++ - wxwidget wxRichTextCtrl编译结果在ld : symbol(s) not found

c++ - 如何打印该系列的第n个数字?

c++ - 仅 header 库中的全局变量

haxe - 从包中获取所有类

c++ - 在 Flashdevelop OpenFL 中编译需要 31 分钟

flash - Flash 10开发需要什么?

enums - 在 Haxe 中迭代枚举摘要

arrays - 在另一个类中访问没有静态引用的数组 (OOP)

c++ - 连接c++服务器时,flash socket发送错误数据