Results 1 to 3 of 3

Thread: pointers gone bad

  1. #1
    Join Date
    Feb 2006
    Posts
    29
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Windows

    Default pointers gone bad

    hi,

    Qt Code:
    1. PrimaryDefs.h
    2. ...
    3. class ArchiveLocUI;
    4. static ArchiveLocUI *MainWid;
    5. ...
    6.  
    7. ArchiveLocUI.cpp
    8.  
    9. #include <PrimaryDefs.h>
    10. ...
    11. MainWid = (ArchiveLocUI *) this;
    12. qDebug("ArchiveLocUI MainWid(%X) name is %s, of type %s",MainWid, MainWid->name(),MainWid->className());
    13. wtabFCALC = new minicalc(wpgFullCalc,"wtabFCALC");
    14. ...
    15.  
    16. minicalc.cpp
    17.  
    18. #include <ArchiveLocUI.h>
    19. ...
    20. qDebug("minicalc MainWid(%X)",MainWid);
    21. qDebug("minicalc MainWid name is %s",MainWid->name());
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: pointers gone bad

    Quote Originally Posted by illuzioner
    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.

  3. The following user says thank you to jacek for this useful post:

    illuzioner (22nd April 2006)

  4. #3
    Join Date
    Feb 2006
    Posts
    29
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: pointers gone bad

    yep, thanks for pointing that out. i was using static instead of extern.

    thanks again,
    lou

Similar Threads

  1. Sorting using qSort(), - if QList contains POINTERS
    By joseph in forum Qt Programming
    Replies: 13
    Last Post: 18th August 2013, 19:55
  2. Problems passing an array of pointers to objects to a function
    By Valheru in forum General Programming
    Replies: 16
    Last Post: 30th June 2008, 01:11
  3. array of pointers to QtcpSocket
    By gQt in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 16th June 2008, 10:15
  4. Use/Misuse of Pointers
    By ct in forum General Programming
    Replies: 12
    Last Post: 9th May 2007, 00:43
  5. K&R's strcpy function - problem with pointers
    By jamadagni in forum General Programming
    Replies: 7
    Last Post: 8th January 2006, 16:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.