[sldev] Why won't isAvatar() work?

Dale Glass dale at daleglass.net
Thu Oct 4 03:48:13 PDT 2007


On Thu, Oct 04, 2007 at 12:12:56AM -0400, Michael Miller wrote:
> Jason,My apologies, but I am unable to understand fully what you mean. I
> understand that a pointer may be pointing to nothing(but even if(&objectp &&
> objectp->isAvatar()) doesn't work). How would I make use of an LLPointer?
> Would this really help?

The problem with manual memory management is that there can be confusion
regarding what is freed when.

For example, the viewer internally keeps tracks of objects which get
constantly created and destroyed. It's possible to get a pointer to an
object which isn't there anymore because it's been free'd. At that
point you have a pointer pointing to something that used to be a
object, but no longer is. Its memory may have well been overwritten
by something completely unrelated.

To prevent that, you can use a smart pointer, which automatically
deletes an object when nothing is pointing at it anymore. This both
prevents leaking memory (not in all cases) and freeing it prematurely.

You declare it as LLPointer<LLViewerObject> ptr = new LLViewerObject();

then you can copy ptr around, and when all copies disappear, the object
will be freed.

Note that for this to work the LLPointer needs to be created during the
object's creation and be the one way to get a pointer to it. You can't
just create a LLPointer from some normal pointer passed to you and have
that work right.

By that I mean that if you write a function like:

void doStuff( LLViewerObject *vo )
{
	LLPointer<LLViewerObject> ptr = vo;
	if ( ptr->isAvatar() )
	{
		llinfos << "is an avatar" << llendl;
	}
}

Then that'll result in the object getting freed when the function
returns, and that would be a very bad thing.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://lists.secondlife.com/pipermail/sldev/attachments/20071004/30add375/attachment.pgp


More information about the SLDev mailing list