Kevin Brosius on Mon, 15 Jul 2002 08:46:26 -0400


[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++)


Walt Mankowski wrote:
> 
> 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;
> 


Surely not?  What's the scope of qlBillBoard?  Wouldn't you expect it to
be a class variable?  If so, it's already typed within the class
definition.

If you do the above, then it would be local to the constructor...

(For help with the segfault, I'd suggest posting a gdb 'bt' of the seg. 
Using the list command will print the relevant code snippet also.)


> 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

-- 
Kevin Brosius

______________________________________________________________________
Philadelphia Linux Users Group       -      http://www.phillylinux.org
Announcements-http://lists.phillylinux.org/mail/listinfo/plug-announce
General Discussion  -  http://lists.phillylinux.org/mail/listinfo/plug