Closed Bug 81289 Opened 23 years ago Closed 23 years ago

nsIDocument::GetDocumentURL() encourages leaky behaviour

Categories

(Core :: Layout, defect, P3)

defect

Tracking

()

VERIFIED FIXED
mozilla0.9.2

People

(Reporter: sfraser_bugs, Assigned: dbaron)

References

Details

Attachments

(4 files)

nsIDocument has a GetDocumentURL method that returns in its return value an 
addreffed nsIURI. LXRing for use finds several leaks, because of the common 
pattern:

nsCOMPtr<nsIURI> uri = doc->GetDocumentURL()
There's a bunch of methods in nsIDocument that returns addref'ed pointers, if we
change one, we should change them all, IMO. I'm all for doing this.
I humbly beg you to save this for 0.9.2 (after I land my branch).  :)
I'm willing to do this if jst isn't, although I'm not completely sure why it was
assigned to me.  I'm *un*realistically marking it 0.9.2.
Status: NEW → ASSIGNED
Priority: -- → P3
Target Milestone: --- → mozilla0.9.2
Would anybody object if I fixed nsIScriptContext::GetGlobalObject at the same time?
nsIScriptContext::GetGlobalObject() too is fine with me.
Index: mozilla/content/base/src/nsContentUtils.cpp

 nsContentUtils::GetDynamicScriptGlobal(JSContext* aContext,
                                       nsIScriptGlobalObject** aNativeGlobal)
 {
-  nsIScriptGlobalObject* nativeGlobal = nsnull;
   nsCOMPtr<nsIScriptContext> scriptCX;
   GetDynamicScriptContext(aContext, getter_AddRefs(scriptCX));
-  if (scriptCX) {
-    *aNativeGlobal = nativeGlobal = scriptCX->GetGlobalObject();
-  }
-  return nativeGlobal ? NS_OK : NS_ERROR_FAILURE;
+  if (!scriptCX)
+    return NS_ERROR_NULL_POINTER;
+  return scriptCX->GetGlobalObject(aNativeGlobal);
 }  

I think you should return NS_ERROR_FAILURE there.
 

Index: mozilla/content/xul/content/src/nsXULTreeElement.cpp

Watch your whitespace.


Index: mozilla/dom/src/base/nsJSEnvironment.cpp

+NS_IMETHODIMP
+nsJSContext::GetGlobalObject(nsIScriptGlobalObject** aGlobalObject)
 {
   JSObject *global = ::JS_GetGlobalObject(mContext);
+  *aGlobalObject = nsnull;
 
   if (!global) {
-    return nsnull;
+    return NS_ERROR_NULL_POINTER;

Here too just return NS_ERROR_FAILURE.

 if (!c || ((~c->flags) & (JSCLASS_HAS_PRIVATE |
                             JSCLASS_PRIVATE_IS_NSISUPPORTS))) {
-    return nsnull;
+    return NS_ERROR_NULL_POINTER;

And here too.

+  if (!native) {
+    return NS_ERROR_NULL_POINTER;
   }

...



Index: mozilla/dom/src/base/nsJSUtils.cpp

see nsContentUtils.



Index: mozilla/layout/xul/base/src/outliner/src/nsOutlinerSelection.cpp

whitespace...


I'm still not completely sure whether I should be returning NS_OK with a null
out param or whether I should be returning a failure status.  Perhaps it depends
on whether null is normal or an error, but I'm not sure what it is in these
cases.  As jag pointed out, there might also be some places where I could
propagate return values better.
IMO returning NS_OK in all (there might be some exceptions, of course) failure
cases in the implementations that are changed here is the right thing to do,
that's exactly what the old code did (effectively). For instance, I don't think
nsJSContext::GetGlobalObject() should ever return anything but NS_OK, the
question we're asking is, give me the nsIScriptGlobalObject that's the private
data in the global object in the context, if there is no global object, or if
the private data in the global is not of the currect type we simply give back a
null nsIScriptGlobalObject and return NS_OK. That will answer the same question
the old code answered, without intorducing random "exceptions". Only return
error codes in truly exceptional circumstances, such as failure to allocate
memory n' so on...

You might choose to warn (i.e. NS_WARNING) if there's no private data in the
global and in all the other cases where you currently return error codes. This
would help developers debug potential problems. Think of returning an error code
as if you'd be throwing an exception if we'd support exceptions, would it make
sense to throw an exception in all the cases where where we currently return
error codes (not only in this patch), I don't think so.

More comments coming up...
nsContentUtils::GetDynamicScriptGlobal() needs to initialize *aNativeGlobal to
nsnull (it didn't before, but it should), and IMO that method should never
return a failure code either, but let's not change that now...

nsJSContext::GetGlobalObject() should always return NS_OK.

Other than that, sr=jst
OS: Mac System 8.5 → All
Blocks: 83989
r=jag
sr=jst, check this in!
a= asa@mozilla.org for checkin to the trunk.
(on behalf of drivers)
Fix checked in 2001-06-19 20:26/20:27 PDT.
Status: ASSIGNED → RESOLVED
Closed: 23 years ago
Resolution: --- → FIXED
Marking verified per last comments.
Status: RESOLVED → VERIFIED
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: