Hi,

I was doing a code review and noticed something that I'd never noticed before and I wondered if it was 'legal' (aka a good idea) to do it this way. Please let me know what you think.

We have a base class called nsXFormsAccessorsBase which implements the scriptable interface nsIXFormsAccessors. nsXFormsAccessorsBase is a class that isn't meant to be instantiated, but rather it is subclassed by nsXFormsAccessors and nsXFormsControlAccessors. So the base class provides a lot of the functionality for the methods specified in the interface, but the subclasses override a couple of the functions to get the data to return from different places.

So for example, a form author in JS can call var accessors = myfoo.getAccessorsByNode(aNode). Internally this does:

Foo::GetAccessorsByNode(nsIDomNode *aNode, nsIXFormsAccessors **aAccessors)
{
  ...
  *aAccessors = new nsXFormsAccessors(this, aNode);
  if (!*aAccessors)
    return NS_ERROR_OUT_OF_MEMORY;

  NS_ADDREF(*aAccessors);
  return NS_OK;
}

I've never seen code like this in Mozilla, where someone takes the result of a 'new', addrefs it and returns it directly to the calling function (whether it be in c++, JS, etc.). I've debugged this function being called from JS and I see the memory being freed up during the JS garbage collection, so it seems to work and have the proper lifespan and all.

Could someone please verify for me that this is a proper way to do something like this?

Thanks,
--Aaron
_______________________________________________
dev-tech-xpcom mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-xpcom

Reply via email to