我有一个复杂的错误。该软件将PrintParamters发送到打印机几次。在某个时刻,参数结构的所有QStrings都被破坏(坏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(¶m);
}
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有一个0x00000000指针
答案 0 :(得分:4)
您正在堆栈中创建一个结构:RecorderPrintParam param
然后将此结构的地址传递给另一个函数_recorder->setParam(¶m);
当UpdateRecorderPrintParameters
退出param
超出范围且其内容无效时。在堆中分配它并在使用其值完成GUI时释放它,
或者将param
按值传递给setParam
更新此代码还有一个问题是以这种方式创建字符串:
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"