2010-01-21 75 views
0

我有一个复杂的错误。该软件将PrintParamters发送给打印机几次。在某个时刻,参数结构的所有QStrings都被破坏(坏ptr)为什么QString在struct中有时是bad-ptr?

Structs中是否存在QStrings的一般问题?

这里是我使用的结构:

typedef struct RecorderPrintParam { 
    ES_DataType xxxxxxxxxx; 
    bool xxxxxxxxxxx; 
    bool xxxxxxxxxxxx; 
    bool xxxxxxxxxxxx; 
    int  xxxxxxxxxxxxxxxxxxxxxx; 
    double xxxxxxxxxxxxxxx; 
    double xxxxxxxxxx; 
    bool  xxxxxxxxxxx; 
    int xxxxxxxxxxxxxxx; 
    double xxxxxxxxxxx; 
    bool  xxxxxxxxxxx; 
    bool xxxxxxxxxx; 
    double xxxxxxxxx; 
    QString xname; 
    QString yname; 
    QString anotherValue; 
    QString opername; 
    QString region; 
    QString application; 
    QString version; 
    AxisUnit axUnit ; 
    double axLenM; 
    double xxxxxxxx; 
    double xxxxxxxx; 

    int  xxxxxxxx; 
    double xxxxxxxxx; 
    double xxxxxxxxx; 

    bool xxxxxxxxxxxxxxx;/

    double xxxxxxxxxxxxxxx; 

    double xxxxxxxxxx; 
    bool xxxxxxxxx; 

}RecorderPrintParam; 

这里是如何的结构是使用: 从GUI级称为:

void 
MyDlg::UpdateRecorderPrintParameters() 
{ 
     RecorderPrintParam param; 
     .... 
     .... 
     param.xname = QString("abc def 123"); 
     _recorder->setParam(&param); 
} 

param.xname已经有一个不好ascii ptr! ? 我也尝试使用just =“abc def 123”而不是= QString(“abc def 123”);但它的出现同样的错误

这是通过setParam职能的样子:

RecorderInterface::setParam(RecorderPrintParam *up) 
{ 

.... 
... 
if(up->xname.compare(_myParams.xname)!=0) _newHeaderPrint=true; 
... 
... 
} 

} 

的XName仍具有在那一刻“8xname = {d = 0x08e2d568}”的地址,但xname.ascii有一个00000000指针

+0

您还应该发布显示您如何使用此结构的代码。例如它是动态分配的吗? – Alon 2010-01-21 08:39:43

+0

好的,我更新了我的帖子 – Christoferw 2010-01-21 08:51:18

+0

基于这些信息的任何想法? – Christoferw 2010-01-21 09:15:47

回答

4

要在堆栈创建结构:RecorderPrintParam param 然后就通过这一结构的地址给另一个函数_recorder->setParam(&param);

UpdateRecorderPrintParametersë xits param超出范围并且其内容变得无效。它分配在堆并释放它,当该GUI被使用其值,完成 或通过param通过值setParam

UPDATE有与此代码以这种方式创建的字符串一个额外的问题:

QString("abc def 123"); 

创建临时对象,其参考是由重载QString=操作者 C++标准说(12.1)

返回

临时绑定到参考 参数在函数调用中持续存在 直到完成包含调用的完整 表达式。

所以对于QString("abc def 123")对象的析构所述param对象被传递到setParam

尝试之前被调用来改变的QString( “ABC DEF 123”),以QString的STR( “ABC DEF 123”); 和param.xname = str;param.xname = "abc def 123"

+0

我已经测试过通过参数的值,但它导致了同样的错误:( – Christoferw 2010-01-21 09:31:59

+0

param.xname =“abc def 123” - >我已经测试了这个,这会导致同样的错误..我以为相同..... – Christoferw 2010-01-21 14:10:16

+1

我能想到的唯一的其他解释是代码的其他部分会破坏堆栈。 – Alon 2010-01-21 14:51:31

相关问题