Bug 25294

Summary: All WebKit/win classes should return COMPtrs from their static constructor members
Product: WebKit Reporter: Adam Roben (:aroben) <aroben>
Component: WebKit Misc.Assignee: Nobody <webkit-unassigned>
Status: NEW ---    
Severity: Normal CC: adachan, sfalken
Priority: P2 Keywords: InRadar, PlatformOnly
Version: 528+ (Nightly build)   
Hardware: PC   
OS: Windows XP   
Attachments:
Description Flags
patch for MemoryStream
none
Make CFDictionaryPropertyBag::createInstance return a COMPtr none

Description Adam Roben (:aroben) 2009-04-20 07:42:57 PDT
All classes in WebKit/win should return COMPtrs from their static constructor members (e.g., createInstance()). This will help prevent memory leaks/crashes caused by incorrect ref-count management.
Comment 1 Adam Roben (:aroben) 2009-04-20 07:43:11 PDT
<rdar://problem/6803127>
Comment 2 Adam Roben (:aroben) 2009-04-20 08:05:57 PDT
Created attachment 29616 [details]
patch for MemoryStream
Comment 3 Darin Adler 2009-04-20 09:19:39 PDT
Comment on attachment 29616 [details]
patch for MemoryStream

>  HRESULT STDMETHODCALLTYPE MemoryStream::Clone( 
>      /* [out] */ IStream** ppstm)
>  {
> -    *ppstm = MemoryStream::createInstance(m_buffer);
> +    *ppstm = MemoryStream::createInstance(m_buffer).releaseRef();
> +    // FIXME: MSDN says we should be returning STG_E_INSUFFICIENT_MEMORY instead of E_OUTOFMEMORY here.
>      return (*ppstm) ? S_OK : E_OUTOFMEMORY;

Why not copyRefTo here?

> -    COMPtr<IStream> stream(AdoptCOM, MemoryStream::createInstance(buffer.release()));
> +    COMPtr<MemoryStream> stream = MemoryStream::createInstance(buffer.release());
>      m_view->didReceiveData(stream.get());

Would this read better without the local variable?

> -    COMPtr<MemoryStream> result(AdoptCOM, MemoryStream::createInstance(SharedBuffer::wrapCFData(historyData.get())));
> +    COMPtr<MemoryStream> result = MemoryStream::createInstance(SharedBuffer::wrapCFData(historyData.get()));
>      return result.copyRefTo(stream);

Would this read better without the local variable?

r=me
Comment 4 Adam Roben (:aroben) 2009-04-20 09:23:24 PDT
(In reply to comment #3)
> (From update of attachment 29616 [details] [review])
> >  HRESULT STDMETHODCALLTYPE MemoryStream::Clone( 
> >      /* [out] */ IStream** ppstm)
> >  {
> > -    *ppstm = MemoryStream::createInstance(m_buffer);
> > +    *ppstm = MemoryStream::createInstance(m_buffer).releaseRef();
> > +    // FIXME: MSDN says we should be returning STG_E_INSUFFICIENT_MEMORY instead of E_OUTOFMEMORY here.
> >      return (*ppstm) ? S_OK : E_OUTOFMEMORY;
> 
> Why not copyRefTo here?

I originally thought that copyRefTo's behavior was incompatible with the existing behavior of this function. But now I see that it isn't. I will change to use copyRefTo.

> > -    COMPtr<IStream> stream(AdoptCOM, MemoryStream::createInstance(buffer.release()));
> > +    COMPtr<MemoryStream> stream = MemoryStream::createInstance(buffer.release());
> >      m_view->didReceiveData(stream.get());
> 
> Would this read better without the local variable?

Probably. I'll remove the variable.

> > -    COMPtr<MemoryStream> result(AdoptCOM, MemoryStream::createInstance(SharedBuffer::wrapCFData(historyData.get())));
> > +    COMPtr<MemoryStream> result = MemoryStream::createInstance(SharedBuffer::wrapCFData(historyData.get()));
> >      return result.copyRefTo(stream);
> 
> Would this read better without the local variable?

Maybe. I'll remove the variable and see how it looks.

Thanks for the review!
Comment 5 Adam Roben (:aroben) 2009-04-20 20:43:09 PDT
Comment on attachment 29616 [details]
patch for MemoryStream

Landed as r42692
Comment 6 Adam Roben (:aroben) 2009-11-05 10:09:46 PST
Created attachment 42574 [details]
Make CFDictionaryPropertyBag::createInstance return a COMPtr
Comment 7 Adam Roben (:aroben) 2009-11-05 10:17:48 PST
Comment on attachment 42574 [details]
Make CFDictionaryPropertyBag::createInstance return a COMPtr

Landed as r50567 <http://trac.webkit.org/changeset/50567>