views:

261

answers:

2

Hi there!

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 address at that moment"8xname = {d=0x08e2d568 }", but xname.ascii has a 0x00000000 pointer

A: 

(Which version of qt are you using? Later versions suggest you use toAscii and not ascii)

QStrings's are implicitly shared, this means you can inadvertently clobber the strings elsewhere.

Best would be to make explicit copies of such strings and pass the copy when they may be changed.

slashmais
I'm using QT 3.3
Christoferw
+3  A: 

you are creating a structure in the stack : RecorderPrintParam param and then you pass the address of this structure to another fucntion _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 additonal 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"

Alon
I've tested to pass the param by value, but it results into the same error :(
Christoferw
param.xname = "abc def 123" ->i've already teste this, this restults the same error.. i thought the same.....
Christoferw
The only other explanation I can think of is that some other part of your code is corrupting the stack.
Alon
@Alon : for my own knowledge, I don't understand the part following your update :RecorderPrintParam param;param.xname = QString("abc def 123");_recorder->setParam(If I understand your comment correctly, this code snippet is buggy ???
Jérôme