开发者

Why have QString in struct sometimes a bad-ptr?

开发者 https://www.devze.com 2022-12-17 20:48 出处:网络
I\'ve got an complicated error. The software send PrintParamters to a Printer a couple of times. At a certain moment all QStrings of the Parameter Struct are broken (bad ptr)

I've got an complicated error. The software send PrintParamters to a Printer a couple of times. At a certain moment all QStrings of the Parameter Struct are broken (bad ptr)

Is there an general issue with QStrings in Structs?

here is the struct I'm using:

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;

Here is how the struct is been used: called from a GUI-Class:

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

param.xname already has a bad ascii ptr !! ? I also tried to use just = "abc def 123" instead of = QString("abc def 123"); but it's the same error that occurs

This is how the setParam functions looks like:

RecorderInterface::setParam(RecorderPrintParam *up)
{

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

}

xname has still an ad开发者_开发知识库dress at that moment"8xname = {d=0x08e2d568 }", but xname.ascii has a 0x00000000 pointer


you are creating a structure in the stack : RecorderPrintParam param and then you pass the address of this structure to another function _recorder->setParam(&param);

when UpdateRecorderPrintParameters exits param goes out of scope and its content becomes invalid. Allocate it in the heap and release it when the GUI is done using its values, or pass param by value to setParam

UPDATE there is an additional issue with this code creating a string in this manner :

QString("abc def 123"); 

creates a temporary object, whose reference is returned by the overloaded QString = operator the C++ standards say (12.1)

a temporary bound to a reference parameter in a function call persists until the completion of the full expression containing the call.

so the destructor for the QString("abc def 123") object is called before the param object is is passed to setParam

try to change QString("abc def 123") to QString str("abc def 123"); and param.xname = str; or param.xname = "abc def 123"

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号