From 0e6d8ebdba11126ffad3927472aa05a6ad1075d3 Mon Sep 17 00:00:00 2001 From: "waterson%netscape.com" Date: Tue, 4 May 1999 03:32:12 +0000 Subject: [PATCH] Bug 5679. Fix leak from XPConnect glue. --- dom/src/base/nsJSUtils.cpp | 17 +++++++++++------ dom/src/xul/nsJSXULElement.cpp | 2 +- dom/src/xul/nsJSXULTreeElement.cpp | 4 ++-- dom/tools/JSStubGen.cpp | 11 +++++++++-- rdf/content/src/nsJSXULElement.cpp | 2 +- rdf/content/src/nsJSXULTreeElement.cpp | 4 ++-- 6 files changed, 26 insertions(+), 14 deletions(-) diff --git a/dom/src/base/nsJSUtils.cpp b/dom/src/base/nsJSUtils.cpp index ad91ba8c3eef..a93578697700 100644 --- a/dom/src/base/nsJSUtils.cpp +++ b/dom/src/base/nsJSUtils.cpp @@ -152,22 +152,27 @@ nsJSUtils::nsConvertXPCObjectToJSVal(nsISupports* aSupports, JSContext* aContext, jsval* aReturn) { - nsresult rv; + *aReturn = JSVAL_NULL; // a sane value, just in case something blows up + if (aSupports != nsnull) { + nsresult rv; + NS_WITH_SERVICE(nsIXPConnect, xpc, kXPConnectCID, &rv); + if (NS_FAILED(rv)) return; - *aReturn = nsnull; // a sane value, just in case something blows up - - NS_WITH_SERVICE(nsIXPConnect, xpc, kXPConnectCID, &rv); - if (NS_SUCCEEDED(rv)) { nsIXPConnectWrappedNative* wrapper; - nsresult rv = xpc->WrapNative(aContext, aSupports, aIID, &wrapper); + rv = xpc->WrapNative(aContext, aSupports, aIID, &wrapper); if (NS_SUCCEEDED(rv)) { JSObject* obj; rv = wrapper->GetJSObject(&obj); if (NS_SUCCEEDED(rv)) { + // set the return value *aReturn = OBJECT_TO_JSVAL(obj); } NS_RELEASE(wrapper); } + // Yes, this is bizarre, but since this method used in a very + // specific ways in idlc-generated code, it's okay. And yes, it's + // really the semantics that we want. + NS_RELEASE(aSupports); } } diff --git a/dom/src/xul/nsJSXULElement.cpp b/dom/src/xul/nsJSXULElement.cpp index db1c4bba75f4..dc1646c407c9 100644 --- a/dom/src/xul/nsJSXULElement.cpp +++ b/dom/src/xul/nsJSXULElement.cpp @@ -72,7 +72,7 @@ GetXULElementProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp) { nsIRDFResource* prop; if (NS_OK == a->GetResource(&prop)) { - // get the js object + // get the js object; n.b., this will do a release on 'prop' nsJSUtils::nsConvertXPCObjectToJSVal(prop, nsIRDFResource::GetIID(), cx, vp); } else { diff --git a/dom/src/xul/nsJSXULTreeElement.cpp b/dom/src/xul/nsJSXULTreeElement.cpp index b7750281cc25..5c355fb13ae8 100644 --- a/dom/src/xul/nsJSXULTreeElement.cpp +++ b/dom/src/xul/nsJSXULTreeElement.cpp @@ -66,7 +66,7 @@ GetXULTreeElementProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp) { nsIRDFCompositeDataSource* prop; if (NS_OK == a->GetDatabase(&prop)) { - // get the js object + // get the js object; n.b., this will do a release on 'prop' nsJSUtils::nsConvertXPCObjectToJSVal(prop, nsIRDFCompositeDataSource::GetIID(), cx, vp); } else { @@ -110,7 +110,7 @@ SetXULTreeElementProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp) } a->SetDatabase(prop); - + NS_IF_RELEASE(prop); break; } default: diff --git a/dom/tools/JSStubGen.cpp b/dom/tools/JSStubGen.cpp index 5149685045a1..d63b9114aa52 100644 --- a/dom/tools/JSStubGen.cpp +++ b/dom/tools/JSStubGen.cpp @@ -573,7 +573,7 @@ static const char *kObjectGetCaseStr = " nsJSUtils::nsConvertObjectToJSVal((nsISupports *)prop, cx, vp);\n"; static const char *kXPIDLObjectGetCaseStr = -" // get the js object\n" +" // get the js object; n.b., this will do a release on 'prop'\n" " nsJSUtils::nsConvertXPCObjectToJSVal(prop, %s::GetIID(), cx, vp);\n"; static const char *kStringGetCaseStr = @@ -706,6 +706,8 @@ static const char *kXPIDLObjectSetCaseStr = static const char *kObjectSetCaseEndStr = "NS_IF_RELEASE(prop);"; +static const char* kXPIDLObjectSetCaseEndStr = kObjectSetCaseEndStr; + static const char *kStringSetCaseStr = " nsJSUtils::nsConvertJSValToString(prop, cx, *vp);\n"; @@ -773,7 +775,11 @@ JSStubGen::GeneratePropSetter(ofstream *file, break; } - end_str = aAttribute.GetType() == TYPE_OBJECT ? kObjectSetCaseEndStr : ""; + switch (aAttribute.GetType()) { + case TYPE_OBJECT: end_str = kObjectSetCaseEndStr; break; + case TYPE_XPIDL_OBJECT: end_str = kXPIDLObjectSetCaseEndStr; break; + default: end_str = ""; break; + } if (aIsPrimary) { sprintf(buf, kSetCaseStr, attr_type, case_buf, attr_name, end_str); } @@ -1007,6 +1013,7 @@ static const char *kMethodObjectRetStr = " nsJSUtils::nsConvertObjectToJSVal(nativeRet, cx, rval);\n"; static const char *kMethodXPIDLObjectRetStr = +" // n.b., this will release nativeRet\n" " nsJSUtils::nsConvertXPCObjectToJSVal(nativeRet, %s::GetIID(), cx, rval);\n"; static const char *kMethodStringRetStr = diff --git a/rdf/content/src/nsJSXULElement.cpp b/rdf/content/src/nsJSXULElement.cpp index db1c4bba75f4..dc1646c407c9 100644 --- a/rdf/content/src/nsJSXULElement.cpp +++ b/rdf/content/src/nsJSXULElement.cpp @@ -72,7 +72,7 @@ GetXULElementProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp) { nsIRDFResource* prop; if (NS_OK == a->GetResource(&prop)) { - // get the js object + // get the js object; n.b., this will do a release on 'prop' nsJSUtils::nsConvertXPCObjectToJSVal(prop, nsIRDFResource::GetIID(), cx, vp); } else { diff --git a/rdf/content/src/nsJSXULTreeElement.cpp b/rdf/content/src/nsJSXULTreeElement.cpp index b7750281cc25..5c355fb13ae8 100644 --- a/rdf/content/src/nsJSXULTreeElement.cpp +++ b/rdf/content/src/nsJSXULTreeElement.cpp @@ -66,7 +66,7 @@ GetXULTreeElementProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp) { nsIRDFCompositeDataSource* prop; if (NS_OK == a->GetDatabase(&prop)) { - // get the js object + // get the js object; n.b., this will do a release on 'prop' nsJSUtils::nsConvertXPCObjectToJSVal(prop, nsIRDFCompositeDataSource::GetIID(), cx, vp); } else { @@ -110,7 +110,7 @@ SetXULTreeElementProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp) } a->SetDatabase(prop); - + NS_IF_RELEASE(prop); break; } default: