You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by Jeff Lewis <jd...@xactware.com> on 2000/06/01 02:33:06 UTC
References and pointers to DOM_????
In the documentation on the web for Xerces-C, it says that no one should
ever pass a DOM_???? object as a pointer or by reference. I understand how,
semantically, it makes sense not to do that: these objects are really just
shells, SmartPointers, that contain a pointer to the real thing. But I have
been trying to find a reason where passing by reference might fail...
Specifically, we have a lot of inline functions where we really don't want
to have to take the overhead of incrementing the reference count, etc that
does occur on a regular pass by value of a DOM_Node-dirived class.
Where is the problem? Can someone show me with a few lines of code?
Here are two examples functions:
INLINE U8 ImportU8FromXML(const DOM_Element &ele, const P_C_CHAR name)
{
U8 temp;
ImportFromXML(ele, temp, name);
return temp;
}
INLINE FLAG8 ImportFLAG8FromXML(const DOM_Element &ele, const P_C_CHAR name)
{
FLAG8 temp;
ImportFromXML(ele, temp, name);
return temp;
}
Re: References and pointers to DOM_????
Posted by Michael Mason <mg...@decisionsoft.com>.
> Jeff Lewis wrote:
>
> In the documentation on the web for Xerces-C, it says that no one
> should ever pass a DOM_???? object as a pointer or by reference. I
> understand how, semantically, it makes sense not to do that: these
> objects are really just shells, SmartPointers, that contain a pointer
> to the real thing. But I have been trying to find a reason where
> passing by reference might fail...
>
> Specifically, we have a lot of inline functions where we really don't
> want to have to take the overhead of incrementing the reference count,
> etc that does occur on a regular pass by value of a DOM_Node-dirived
> class.
I've been passing DOM_xxx by reference, and have had no problems with it
so far. I'd also like to know if there's theoretical problem with this
kind of style.
Cheers,
Mike.
--
Mike Mason, Software Engineer
XML Script Development Team Office: 44-1865-203192
http://www.xmlscript.org/ Mobile: 44-7050-288923
Passing DOMString by reference?
Posted by Richard Lanyon <rg...@decisionsoft.com>.
There's been a lot of talk on this list recently about the way that
most of the DOM classes (the ones beginning 'DOM_') are really smart
pointers, and how it is often best to pass them by value rather than
by reference. What I was wondering was whether similar considerations
apply to the DOMString class, which also appears to have
reference-counted memory management.
--
Richard Lanyon
Re: References and pointers to DOM_????
Posted by Andy Heninger <an...@jtcsv.com>.
References and pointers to DOM_????Never passing a DOM_Node (or derived class) object by pointer or reference may be a bit strong, but is safe.
Passing a DOM_Node & reference as a parameter to a function that uses the node, but does not permanently hold onto it, is perfectly safe, and more effecient than passing by value.
Functions like those shown in the note below are fine.
The thing to avoid at all costs is saving a pointer or reference to a DOM_Node object in some structure with a lifetime that exceeds that of the function being called. Two things are likely problems here:
1. The DOM_Node (or Element or whatever) is someone elses proxy to the
actual node. Its lifetime is not that of the node itself, and,
in all probablility, it's just some other function's local variable
that will disappear long before you expected, and leave you with a
dangling pointer to some random stack location.
2. Avoiding the above problem requires doing conventional C / C++
storage management - knowing who allocated the DOM_Element, who
is holding pointers to it, and who will be responsible for
deleting it when the time comes. There is no point to any of
this - if everyone holds copies by value rather than pointers
or references, all storage management problems are handled
automatically.
A function that has been passed a DOM_Node & reference parameter can permanently hold a copy of that DOM_Node object, just not a pointer to the one originally passed.
Andy Heninger
IBM XML Technology Group, Cupertino, CA
heninger@us.ibm.com
----- Original Message -----
From: Jeff Lewis
To: xerces-c-dev@xml.apache.org
Sent: Wednesday, May 31, 2000 5:33 PM
Subject: References and pointers to DOM_????
In the documentation on the web for Xerces-C, it says that no one should ever pass a DOM_???? object as a pointer or by reference. I understand how, semantically, it makes sense not to do that: these objects are really just shells, SmartPointers, that contain a pointer to the real thing. But I have been trying to find a reason where passing by reference might fail...
Specifically, we have a lot of inline functions where we really don't want to have to take the overhead of incrementing the reference count, etc that does occur on a regular pass by value of a DOM_Node-dirived class.
Where is the problem? Can someone show me with a few lines of code?
Here are two examples functions:
INLINE U8 ImportU8FromXML(const DOM_Element &ele, const P_C_CHAR name)
{
U8 temp;
ImportFromXML(ele, temp, name);
return temp;
}
INLINE FLAG8 ImportFLAG8FromXML(const DOM_Element &ele, const P_C_CHAR name)
{
FLAG8 temp;
ImportFromXML(ele, temp, name);
return temp;
}