[patch] to bug 88243

Koos Vriezen koos.vriezen at xs4all.nl
Thu Feb 10 23:25:28 GMT 2005


On Fri, Feb 11, 2005 at 12:16:35AM +0100, Paul Temple wrote:
> > Looks to me you take an adress of a temporary with
> >   Node* myNode = &(toNode(args[0]));
> > that should be written as
> >   Node myNode = toNode(args[0]);
> > which also looks a lot less scary :-)
> 
> Yes, there are warnings, but _I_ (unlike the compiler) know that 
> I don't do any harm... :)

And I know that you do :-)
toNode returns a DOM::Node, that's an object. This object is immediately
destroyed after the assignment (the = operation). So you end with an
adress of an deleted object. If written as 'Node myNode = toNode' it
creates a new object with the returned object of toNode as argument
(that's the copy contructor 'Node(const Node & other)'.

Actually now that I'm thinking about this, you never should take an
address of a returned value other than that a function returns a
reference, eg 'MyObject& myfunct() { return myobject; }'

> I wrote that with my little c++ knowledge... I thought that
> "Node myNode" would allocate memory which was not necessary as 
> this is done in "toNode(..)" anyway.

Yes, but that one gets destroyed.

> Why does 
> >   Node myNode = toNode(args[0]);
> look scary? Does it allocate never used memory or not?

less scary you mean. Because of the above explaination.

Koos




More information about the kfm-devel mailing list