Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 6

Qt-interest Archive, August 2006
problem with constructor (non-html)


Message 1 in thread

(sorry about that.  accidentally sent HTML-formatted post.  just in case that's going to be a problem, i'm resending as plain-text)

i think i've run into this issue before, but it was a long time ago and i can't remember what the problem turned out to be.

i've implemented a class with a constructor that takes as one of its arguments a pointer to a QWidget (ie: QWidget * thewidget).

inside the constructor i do something like this:

    thewidget = new QWidget;

that goes fine: memory is properly allocated and the pointer is valid... that is until i return out of the constructor back to the calling code.  if i then try to use the new instance of the QWidget, i get a segmentation fault.

printing out the pointer address before the constructor call, inside the constructor (just after calling new QWidget), and just after the call to the constructor, shows that the pointer is first NULL, then valid, then NULL again (respectively).  if i don't initialize the pointer before the call, it's junk, then valid, then the exact same junk again (respectively).

please point out what i'm doing wrong here:

Thingy::Thingy ( QWidget * thewidget )
: ThingysMommy()
{
    ......
    printf("B (%p)\n", thewidget);
    fflush(stdout);

    thewidget = new QWidget;

    printf("C (%p)\n", thewidget);
    fflush(stdout);
    ......
}


... and the calling code looks like:

    ......
    QWidget * widget;

    printf("A (%p)\n", widget);
    fflush(stdout);

    Thingy thing(widget);

    printf("D (%p)\n", widget);
    fflush(stdout);

    widget->doSomething();   // causes segmentation fault
    ......



... the output looks like:

A (0x44002442)                   // uninitialized junk
B (0x44002442)                   // uninitialized junk
C (0x10043C98)                   // valid
D (0x44002442)                   // back to exact same uninitialized junk


how do i get the address to stick.

thanks.

- chase




--
 [ signature omitted ] 

Message 2 in thread

> From: invest [mailto:invest@xxxxxxxx]
> Sent: Friday, August 25, 2006 8:39 PM
> 
> (sorry about that.  accidentally sent HTML-formatted post.  just in
case
> that's going to be a problem, i'm resending as plain-text)
> 
> i think i've run into this issue before, but it was a long time ago
and i
> can't remember what the problem turned out to be.
> 
> i've implemented a class with a constructor that takes as one of its
> arguments a pointer to a QWidget (ie: QWidget * thewidget).
> 
> inside the constructor i do something like this:
> 
>     thewidget = new QWidget;
> 
> that goes fine: memory is properly allocated and the pointer is
valid...
> that is until i return out of the constructor back to the calling
code.
> if i then try to use the new instance of the QWidget, i get a
segmentation
> fault.
> 
> printing out the pointer address before the constructor call, inside
the
> constructor (just after calling new QWidget), and just after the call
to
> the constructor, shows that the pointer is first NULL, then valid,
then
> NULL again (respectively).  if i don't initialize the pointer before
the
> call, it's junk, then valid, then the exact same junk again
> (respectively).
> 
> please point out what i'm doing wrong here:
> 
> Thingy::Thingy ( QWidget * thewidget )
> : ThingysMommy()
> {
>     ......
>     printf("B (%p)\n", thewidget);
>     fflush(stdout);
> 
>     thewidget = new QWidget;
> 
>     printf("C (%p)\n", thewidget);
>     fflush(stdout);
>     ......
> }
> 
> 
> ... and the calling code looks like:
> 
>     ......
>     QWidget * widget;
> 
>     printf("A (%p)\n", widget);
>     fflush(stdout);
> 
>     Thingy thing(widget);
> 
>     printf("D (%p)\n", widget);
>     fflush(stdout);
> 
>     widget->doSomething();   // causes segmentation fault
>     ......

You're missing a basic understanding of pointers.  When you pass a
pointer to a function, you can change the object that the pointer points
to, but not what the pointer is.  

The thewidget pointer is a copy of the widget pointer that you passed to
the constructor.  Initially, they point to the same (non initialized)
memory.  Then you change the copy inside the constructor.

You have two options, if I can guess what you're trying to do.

Thingy thing;
widget = thing.makeAWidget();

or

Thingy::Thingy(QWidget*& thewidget)

I suggest you get a lot more friendly with the concept of pointers,
generally for c++ coding, and specifically if you're going to be using
Qt, as they are everywhere in Qt.
-----------------------------------------------------------------------
DISCLAIMER:  Information contained in this message and/or attachment(s) may contain confidential information of Zetec, Inc. If you have received this transmission in error, please notify the sender by return email.

Ce courrier électronique est confidentiel et protégé. L'expéditeur ne renonce pas aux droits et obligations qui s'y rapportent. Toute diffusion, utilisation ou copie de ce message ou des renseignements qu'il contient par une personne autre que le (les) destinataire(s) désigné(s) est interdite. Si vous recevez ce courrier électronique par erreur, veuillez m'en aviser immédiatement, par retour de courrier électronique ou par un autre moyen.
-----------------------------------------------------------------------

--
 [ signature omitted ]