Closed Bug 59414 Opened 26 years ago Closed 25 years ago

misuse of nsCOMPtr::operator&

Categories

(Core :: XPCOM, defect, P3)

x86
Linux
defect

Tracking

()

RESOLVED FIXED
mozilla0.8

People

(Reporter: dbaron, Assigned: dbaron)

References

Details

(Whiteboard: [xptest])

Attachments

(21 files)

1.56 KB, patch
Details | Diff | Splinter Review
13.80 KB, patch
Details | Diff | Splinter Review
13.80 KB, patch
Details | Diff | Splinter Review
103.98 KB, patch
Details | Diff | Splinter Review
103.36 KB, patch
Details | Diff | Splinter Review
2.35 KB, patch
Details | Diff | Splinter Review
1.90 KB, patch
Details | Diff | Splinter Review
16.30 KB, patch
Details | Diff | Splinter Review
2.03 KB, patch
Details | Diff | Splinter Review
3.18 KB, patch
Details | Diff | Splinter Review
3.06 KB, patch
Details | Diff | Splinter Review
3.91 KB, patch
Details | Diff | Splinter Review
16.37 KB, patch
Details | Diff | Splinter Review
16.91 KB, patch
Details | Diff | Splinter Review
3.26 KB, patch
Details | Diff | Splinter Review
2.19 KB, patch
Details | Diff | Splinter Review
2.49 KB, patch
Details | Diff | Splinter Review
926 bytes, patch
Details | Diff | Splinter Review
743 bytes, patch
Details | Diff | Splinter Review
3.68 KB, patch
Details | Diff | Splinter Review
881 bytes, patch
Details | Diff | Splinter Review
There's a bit of code that uses & instead of getter_AddRefs. Most of this code is dealing with an nsCOMPtr that is null, so it doesn't leak. However, it's still wrong and it confuses the leak loging. If the nsCOMPtr isn't null, then it's an automatic leak. I'm going to attach: (1) a patch that makes nsCOMPtr::operator& private and provides nsCOMPtr::get_address() for legitimate users (2) a patch that fixes all the incorrect uses of nsCOMPtr::operator& (3) a patch that changes all the legitimate (?) users to use get_address() (2) should definitely get checked in (possibly with modifications, of course). We should discuss what we want to do with (1) and (3).
The only thing I don't like is that |get_address| is a member function. I know this sounds nit-picky, but I would prefer non-member functions like so template <class T> inline nsCOMPtr<T>* address_of( nsCOMPtr<T>& aPtr ) { return &aPtr; } template <class T> inline const nsCOMPtr<T>* address_of( const nsCOMPtr<T>& aPtr ) { return &aPtr; } These two template functions would be |friend|s of |nsCOMPtr|. It's silly, I know. I just prefer |address_of(fooP)| over |fooP.get_address()|. I could probably be argued out of this ... but I have always disliked excessive member functions in smart-pointers, because they break you out of the idiom of a smart pointer. I made |get| a member, because that is a member of |std::auto_ptr|, but when faced with adding new members, I put |getter_AddRefs|, et al, outside the class. The reason I could possibly be argued out of this is because we may _want_ to break out of the idiom to prevent this bug ... it just makes me feel oogy.
*** Bug 59069 has been marked as a duplicate of this bug. ***
Changing all the usage to address_of() was easy -- I just had to do a search replace on the patch, unapply the old one, and apply the new one. However, I couldn't get the necessary changes to nsCOMPtr.h to compile. I had to introduce a bunch of forward declarations (which I suspect may not be portable, considering some of the other comments in nsCOMPtr.h), but I still failed with: /builds/seamonkey/mozilla/xpcom/base/nsCOMPtr.h: In function `nsCOMPtr<T> *address_of (nsCOMPtr<T> &) [with T = nsISupports]': /builds/seamonkey/mozilla/xpcom/base/nsCOMPtr.h:994: instantiated from here /builds/seamonkey/mozilla/xpcom/base/nsCOMPtr.h:838: `const nsCOMPtr<nsISupports> *nsCOMPtr<nsISupports>::operator& () const' is private /builds/seamonkey/mozilla/xpcom/base/nsCOMPtr.h:902: within this context /builds/seamonkey/mozilla/xpcom/base/nsCOMPtr.h:902: cannot convert `const nsCOMPtr<nsISupports> *' to `nsCOMPtr<nsISupports> *' in return The changes I used are attached above.
And once I fix the obvious mistake (s/nsISupports/T/g) in the forward declarations, I get different errors: /builds/seamonkey/mozilla/xpcom/base/nsCOMPtr.h: In function `nsCOMPtr<T> *address_of (nsCOMPtr<T> &) [with T = nsISupports]': /builds/seamonkey/mozilla/xpcom/base/nsCOMPtr.h:994: instantiated from here /builds/seamonkey/mozilla/xpcom/base/nsCOMPtr.h:902: cannot convert `const nsCOMPtr<nsISupports> *' to `nsCOMPtr<nsISupports> *' in return
This may sound goofy, but you could keep the public member function |get_address| and implement |address_of| in terms of |get_address|. This is the equivalent of how |getter_AddRefs|, et al, work. ... and for just the reason you're encountering.
exactly ... though you might want to remove the rogue |&| in the body of the |const| version of |address_of|. You should also add yourself to the contributors list at the top of the file :-)
To eliminate |const| warnings (which you might not have noticed because of selective template instantiation), you may want to either provide _two_ forms of |get_address| for each of |nsCOMPtr<T>| and |nsCOMPtr<nsISupports>| (my recommendation), or else use a |NS_CONST_CAST| in the |const| version of |address_of| --- you'll need to cast away |const|-ness to call your non-|const| version of |get_address|. To match my formatting, you should put the return value of |get_address| on the a separate line. While we're in there, we should decide if we want anyone to ever use |&| (i.e., |friends|). If not, then there's no reason not to make the definition of |&| be doubly poisonous, like so void operator&() const { } since it doesn't return a value, even if a |friend| tries to use it, it will be a syntax error. If we want to use it ourselves, then we should provide both forms (|const| and non-|const|). I recommend the poisonous form, which is normally how I turn off |operator=| et al. Sooner or later, someone is going to add new code that says |&some_nsCOMPtr| and be surprised when it doesn't compile. We'll want to put a big comment by |operator&| saying why we made |private|, and to prefer (first) passing by reference, or else (second) using |address_of|. I've updated the |nsCOMPtr| User's Guide to add a section talking about using |nsCOMPtr|s in APIs <http://www.mozilla.org/projects/nsCOMPtr.html#guide_nsCOMPtrs_in_APIs> and it mentions |address_of| and this bug, as well; so let's make this come true soon :-) We should probably put the above link into the comment at |operator&| to point confused readers at the rules for passing |nsCOMPtr|s around. If you have any concerns at all about this being ready to checkin ... here is the order to do it in that will make life the best: (1) check in all the changes to |nsCOMPtr| _except_ making |operator&| |private| ... now |address_of| is available to anyone who knows what an |nsCOMPtr| is (2) check in, at your own convenience, all your changes switching clients from |&| to |address_of| (3) when no ordinary callers of |&| remain ... make |operator&| |private|
sr=scc. Now we just have to figure out who else is enough of a C++ weenie to review this stuff. If anybody else had done this work, it would have been me and dbaron reviewing it :-)
To clarify, my [s]r=scc is for "11/14/00 13:48 (1)[ver 4]..." changes attached here <http://bugzilla.mozilla.org/showattachment.cgi?attach_id=19230>
This code makes sense to me, r=jag. One of these days I'll be that C++ weenie ;-)
(1)[ver 4] (though I included my changed comments) builds and runs on Mac, David. You've tested linux, I imagine. That's two platforms ... probably enough, but if you can get someone to build and run on Windows then it's a no-brainer.
r=jag on (3)[ver.2] and (2)[ver 2]
Two issues jag and I discussed on IRC while he was reviewing: * I changed nsExpatTokenizer.cpp not to use nsCOMPtr because nsCOMPtr doesn't have a good way of dealing with in/out parameters. The current code relied on the fact that (nsIURI **)&uri gave &uri->mRawPtr, which seems ugly. * I'm not sure whether it's better style to initialize or not to initialize an |nsresult rv| before calling do_GetService(..., &rv), since do_GetService should always (I think) set rv. I did it some places and not others, but I should probably pick one.
If you want to do in/out parameters, pass the |nsCOMPtr| by reference. Raw pointers make bad in/out parameters, because the ownership model is not specified. Technically, the client code can't know whether to |Release| the old value before assigning in the new. Using |nsCOMPtr| here makes it clear to both caller and callee, and no funny business is required. void use_foo( nsCOMPtr<nsIFoo>& aFoo ) { // ... aFoo = some_new_foo; // old value |Release|d, new value |AddRef|ed // ... }
for the 12/01/00 13:17 (1)[ver 6] patch, r=scc
r=jag on [2](rev 3) and (1)[ver 6].
r=jag on (2)[ver 4]
r=waterosn on (1)[ver 6] and (2)[ver 4].
Ok, treat above r= as sr=, and also sr=waterson on (3)[ver. 2].
Status: NEW → ASSIGNED
Target Milestone: --- → mozilla0.9
seems like that _should_ fix it; you don't need an r= to fix bustage ... but you've got one anyway. This is temporary no matter what, since the final change will compile everywhere with only one instance.
Sorry David, completely forgot to jot my r= in here. r=jag on "(5) a few address_of changes I missed the first time around".
Apologies -- I forgot to put my sr=brendan@mozilla.org on "(5) a few address_of changes" last week. /be
Yeah, I guess patch (6) <http://bugzilla.mozilla.org/showattachment.cgi?attach_id=20450> is ready. sr=scc
Whiteboard: [xptest]
r=jag on (7)
sr=waterson on `01/08/01 21:32 (7) some new address_of changes'
Patch #6 built and ran for sfraser on Mac with no changes.
Patch #6 built for bryner on Windows with the one change I will attach below.
r=jag on #8 and #6.
r=jag on #9 and #10
Fix checked in 2001-02-04, although I think there may still be some incorrect usage in the commercial tree that was patch with address_of().
Status: ASSIGNED → RESOLVED
Closed: 25 years ago
Resolution: --- → FIXED
Target Milestone: mozilla0.9 → mozilla0.8
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: