字符串赋值崩溃 [英] string assignment crash

查看:119
本文介绍了字符串赋值崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



编写整个代码块很困难,因为它太长了。我只是想说它在字符串赋值部分崩溃了。有一个枚举,我将字符串值赋给枚举变量。



回溯如下;





Hi,
It is difficult to write the whole code block, because it is too long. I just want to say that it crashes in string assignment part. There is an enum, and I assign the string value to enum variables.

The backtrace is as follows ;


# BACKTRACE:
0./lib/libc.so.0(kill+0x24) [2bb42730]
1./lib/libpthread.so.0(pthread_kill+0xa8) [2b9dee58]
2./lib/libpthread.so.0(raise+0x34) [2b9df42c]
3./lib/libc.so.0(abort+0xac) [2bb39b40]
4./lib/libstdc++.so.6(__gnu_cxx::__verbose_terminate_handler()++.so.6
5./lib/libstdc++.so.6(__cxxabiv1::__terminate(void (*)())++.so.6
6./lib/libstdc++.so.6(std::terminate()++.so.6
7./lib/libstdc++.so.6(__cxa_throw+0xb8) [2ba8a4bc]
8./lib/libstdc++.so.6(operator new(unsigned int)++.so.6
9./lib/libstdc++.so.6(std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&)++.so.6
10./lib/libstdc++.so.6(std::string::_Rep::_M_clone(std::allocator<char> const&, unsigned int)++.so.6
11./lib/libstdc++.so.6(std::string::assign(std::string const&)++.so.6





你能帮忙解决一下坠机的原因吗?原因是什么?

提前致谢,





这是代码块。它总是在这两个事件中崩溃;



Could you please help about the cause of crash ? What can be the cause ??
Thanks in advance,


Here is the code block. It always crashes in these two events ;

void event1(){
     {
        tempValue = "";
        if(pName == ReasonTag)
            xmlParamType = ReasonForRequest;
        else if(pName == FailureMsgTag)
            xmlParamType = FailureMessage;
        else if(pName == SchemaTag)
        {
            if(pDataAtts[0] == SchemaNameTag)
                schemaName = pDataAtts[1];
        }
        else if(pName == SurnameTag)
            xmlParamType = ItemSurname;
        else if(pName == FirstnameTag)
            xmlParamType = ItemFirstname;
        else if(pName == OfficeTag)
            xmlParamType = ItemOffice;
        else if(pName == MobileTag)
            xmlParamType = ItemMobile;
        else if(pName == EmailTag)
            xmlParamType = ItemEmail;
        else if(pName == CompanyTag)
            xmlParamType = ItemCompany;
        else if(pName == AddressTag)
            xmlParamType = ItemAddress;
        else if(pName == UniqueIdTag)
            xmlParamType = UniqueId;
        else
            xmlParamType = eParamNull;
 
    }
}
 
    void event2()
    {
        switch(xmlParamType)
    {
        case ItemSurname:
             itemSurname = tempValue;
        break;
 
        case ItemFirstname:
             itemFirstname = tempValue;
        break;
 
        case ItemOffice:
             itemOffice = tempValue;
        break;
 
        case ItemMobile:
             itemMobile = tempValue;
        break;
 
        case ItemPrivate:
             itemPrivate = tempValue;
        break;
 
        case ItemEmail:
             itemEmail = tempValue;
        break;
 
        case ItemCompany:
             itemCompany = tempValue;
        break;
 
        case ItemAddress:
             itemAddress = tempValue;
        break;
 
        case UniqueId:
             uniqueId = tempValue;
        default:
            break;
    }
}









这是变量的定义;







Here is the definitions of variables ;

enum xmlParamType{ ParamNull, ReasonForRequest, SchemaName, ItemSurname, ItemFirstname, ItemOffice, ItemMobile, ItemEmail, ItemCompany, ItemAddress, ItemJobFunction, UniqueId };
OString tempValue;



其他变量都是OString类型变量,如itemSurname,itemFirstName ..



[/ EDIT]


And the other variables are all OString type variables, like itemSurname, itemFirstName ..

[/EDIT]

推荐答案

为类型 std :: string 只有在传递的值被伪装成一个它不是的类型并且包含的​​值不符合与该类型相关的期望时才会崩溃。



在这种情况下,您直接传递类型为 OString 的变量,这意味着类型 OString 具有隐式转换运算符可以分配给 std :: string 变量的类型。很可能这种类型是 const char * 。检查以下运算符的实现:

The assignment of a value to a variable of type std::string can only crash if the value passed is disguised as a type that it isn't and contains value that doesn't fit the expectation tied to that type.

In this case, you are passing variables of type OString directly, implying that the type OString has an implicit conversion operator to a type that can be assigned to a std::string variable. Most likely this type is const char*. Check the implementation of the following operator:
OString::operator const char*() const



实际函数可能缺少一个或两个const,但它必须存在,否则赋值甚至不会编译!在实现代码中,检查返回的内容。



此代码中至少有两件事可能出错:

1。返回一个不正确的值,可能是通过将某些内容输入到 const char *

2.返回未正确初始化或分配的指针;这也可能是由于赋值运算符的代码中的错误。



如果这些提示无法帮助您识别错误,请向我们展示代码相关的 OString 运算符(主要是赋值和转换运算符)。


The actual function may be missing a const or two, but it must exist, or the assignment wouldn't even compile! In the implementation code, check what is returned.

There are at least two things that can go wrong in this code:
1. Returning an incorrect value, possibly by type-casting something to const char*
2. Returning a pointer that wasn't correctly initialized or assigned; this may also be due to an error in the code for the assignment operator.

If these hints don't help you identify the bug, show us the code of the relevant OString operators (assignment and conversion operators, mostly).


调试代码,你会发现将一个字符串分配给一个enum是个坏主意。



枚举是int。
debug the code and you will find out that assign a string to an enum is a real bad idea.

Enums are typed int.


这篇关于字符串赋值崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆