Yes, it actually has 2 references upon exit: The singleton and the
function return value. Presumably the caller to createInstance() also
holds onto a reference when the return value goes out of scope.

I hope there is a mutex protecting the singleton somewhere because
this code has a race condition otherwise.

On May 19, 5:15 am, Freepine <freep...@gmail.com> wrote:
> Its reference count gets increased by copy constructor & operator = while
> returned from createInstance().
>
> On Tue, May 19, 2009 at 4:15 PM, xie <yili....@gmail.com> wrote:
>
> > hi Dianne~~
>
> > thanks for you answer~~ i have implmented the camera hal with v4l2,
> > although i used the below source, i still have some confusion. I think
> > the below source is a singleton mode, if the object have been freed we
> > will creat a new object . when i creat a new object with this code :
>
> > sp<CameraHardwareInterface> hardware(new CameraHal());
>
> > i think the "hardware" is a temp variable, it will be deleted when the
> > function return. And the sp is a smartpointer ,it will free the object.
> > But i am wrong in fact , the object have not been deleted. Can you tell
> > my where i am wrong ~~
>
> > thanks very much
>
> > sp<CameraHardwareInterface> CameraHal::createInstance()
> > {
> >    LOG_FUNCTION_NAME
>
> >    if (singleton != 0) {
> >        sp<CameraHardwareInterface> hardware = singleton.promote();
> >        if (hardware != 0) {
> >            return hardware;
> >        }
> >    }
>
> >    sp<CameraHardwareInterface> hardware(new CameraHal());
>
> >    singleton = hardware;
> >    return hardware;
> > }
>
> > 在 2009-05-19二的 00:43 -0700,Dianne Hackborn写道:
> > > sp == strong pointer, wp == weak pointer.
>
> > > The object will remain around while there are strong pointers; it is
> > > destroyed once the last one is released.  All you can do with a weak
> > > pointer is comparison and attempting to promote to a strong pointer;
> > > the latter will fail if there are no other strong pointers on the
> > > object.
>
> > > On Mon, May 18, 2009 at 11:56 PM, xie <yili....@gmail.com> wrote:
>
> > >         Dear all :
>
> > >         when i read the android source, i find that "<sp>" is not only
> > >         a smart
> > >         pointer,it manage a strong ref count and i also find that the
> > >         <wp>
> > >         manage a weak ref count .
>
> > >         who can tell me how the two kinds of pointer works together,
> > >         when will
> > >         the object be freed?
>
> > >         thanks a lot
>
> > > --
> > > Dianne Hackborn
> > > Android framework engineer
> > > hack...@android.com
>
> > > Note: please don't send private questions to me, as I don't have time
> > > to provide private support, and so won't reply to such e-mails.  All
> > > such questions should be posted on public forums, where I and others
> > > can see and answer them.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"android-framework" group.
To post to this group, send email to android-framework@googlegroups.com
To unsubscribe from this group, send email to 
android-framework+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/android-framework?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to