PDA

View Full Version : pointers gone bad



illuzioner
22nd April 2006, 21:12
hi,


PrimaryDefs.h
...
class ArchiveLocUI;
static ArchiveLocUI *MainWid;
...

ArchiveLocUI.cpp

#include <PrimaryDefs.h>
...
MainWid = (ArchiveLocUI *) this;
qDebug("ArchiveLocUI MainWid(%X) name is %s, of type %s",MainWid, MainWid->name(),MainWid->className());
wtabFCALC = new minicalc(wpgFullCalc,"wtabFCALC");
...

minicalc.cpp

#include <ArchiveLocUI.h>
...
qDebug("minicalc MainWid(%X)",MainWid);
qDebug("minicalc MainWid name is %s",MainWid->name());


a quick look above shows 3 files, 1 header and 2 source. the header file defines a static variable MainWid of type ArchiveLocUI* and this is visible to the whole application. MainWid is initialized first thing in the constructor of ArchiveLocUI and the qDebug shows it has a value (e.g. 12Dff0). a little later in the constructor another widget is created, wtabFCALC of type minicalc* and so its constructor fires.

however, in the constructor of minicalc, MainWid is 0 as printed on line 20 and then the program naturally crashes on line 21. the question is why?? MainWid is global and static and initialized before this line. MainWid is not declared locally anywhere so there's no scope issue.

anyone know what's going on?? thanks!

lou

jacek
22nd April 2006, 21:39
MainWid is global and static and initialized before this line.
Global static variables are global only within single compilation unit and in your case every compilation unit has its own MainWid variable. Better replace that "static" keyword with "extern" or use singleton pattern to access that window.

illuzioner
22nd April 2006, 22:40
yep, thanks for pointing that out. i was using static instead of extern.:o

thanks again,
lou