Walt Mankowski on Mon, 15 Jul 2002 14:30:13 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] [ON TOPIC] passing a pointer to an object to an object (c++)


Note: Resending since my original post Saturday night seems to have
been dropped somewhere...

On Sat, Jul 13, 2002 at 11:26:38AM -0400, Fred K Ollinger wrote:
> I have this code where I have an object and I'd like to modify it while in
> another object. Is there a way to do this? I get segfaults when I do:
> 
> // begin snippet
> voxInstallCommon::voxInstallCommon(QLabel *qlIncoming)
> {
>     qlBillBoard=new QLabel("unlabeled", 0, 0);
>     qlBillBoard=qlIncoming;
> //end snippet

That code segfaults?  I'm surprised it even compiles.  What type is
qlBillBoard?  It looks like it must be QLabel.  Then you either want
to say

  QLabel qlBillBoard = new QLabel("unlabeled", 0, 0);

or

  QLabel qlBillBoard = qlIncoming;

but almost certainly not both.  The first statement allocates space
for a new QLabel object, and assigns a pointer to that space to
qlBillBoard.  The second overwrites the pointer from the first line
with the pointer you passed in as a parameter.  There's no way now to
get at the first pointer anymore, so there's no way to delete it when
you don't need it anymore.  Congratulations, you've created a memory
leak!

I could suggest some correct ways to do that, but I'm not even sure
from that snippet what you're trying to do.  Are you trying to store a
QLabel in a qlBillBoard or vice versa?

Walt

Attachment: pgp6YEMLx9AIO.pgp
Description: PGP signature