MessageLoop with orphaned pointer

23 views
Skip to first unread message

Youngki Chang

unread,
Nov 12, 2012, 11:11:29 AM11/12/12
to chromi...@chromium.org
Hi chromium-dev team,

I have a following class:

class Adapter {
 public:
  Adapter() : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this))  {}
  void Start() {
    Poll();
  }

 private:
  friend class base::RefCounted<Adapter>;
  ~Adapter() {}

  void Poll() {
    // Polling task.

    MessageLoop::current()->PostDelayedTask(
      FROM_HERE,
      base::Bind(&Adapter::Poll, weak_ptr_factory_.GetWeakPtr()),
    base::TimeDelta::FromMilliseconds(500));
  }

  base::WeakPtrFactory<Adapter> weak_ptr_factory_;
};


which basically performs polling task once Start() is called. Let's say Poll() is run and Adapter::Poll of the adapter instance is queued into the current message loop, and then the adapter instance is destroyed by having no more references to it. Does message loop crash due to orphaned pointer, or does message loop handle it gracefully by checking the orphaned pointer and not running it? I made a test to verify this and it seems the message loop handles it gracefully, but I'd like to hear from the expert.


Thanks,


Youngki

Daniel Cheng

unread,
Nov 12, 2012, 11:18:45 AM11/12/12
to you...@chromium.org, Chromium-dev
base::Bind/base::Callback understand base::WeakPtr. If the weak pointer is invalidated, the task will not be executed. See http://dev.chromium.org/developers/coding-style/important-abstractions-and-data-structures#TOC-base::Callback-and-base::Bind- and http://www.chromium.org/developers/design-documents/threading#Scoped_factories for more information.

Daniel


--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev

Mattias Nissler

unread,
Nov 12, 2012, 11:20:16 AM11/12/12
to dch...@chromium.org, you...@chromium.org, Chromium-dev

Youngki Chang

unread,
Nov 12, 2012, 11:34:24 AM11/12/12
to Mattias Nissler, dch...@chromium.org, Chromium-dev
Thanks for the pointers..! :)

Fred Akalin

unread,
Nov 12, 2012, 12:31:50 PM11/12/12
to you...@chromium.org, Mattias Nissler, dch...@chromium.org, Chromium-dev
It looks like your class is ref-counted yet vends weak pointers.  It should not be ref-counted.

Youngki Chang

unread,
Nov 12, 2012, 2:10:34 PM11/12/12
to Fred Akalin, Mattias Nissler, dch...@chromium.org, Chromium-dev
On Mon, Nov 12, 2012 at 12:31 PM, Fred Akalin <aka...@chromium.org> wrote:
It looks like your class is ref-counted yet vends weak pointers.  It should not be ref-counted.

Could you explain why? I thought weak pointers are not counted as references.

Fred Akalin

unread,
Nov 12, 2012, 2:13:29 PM11/12/12
to Youngki Chang, Mattias Nissler, dch...@chromium.org, Chromium-dev
I don't know what it means for weak pointers to "count as references".  But generally, objects are ref-counted only if they're used from multiple threads.  If you have a single thread, it doesn't have to be ref-counted; weak pointers already solve the problem of callbacks outliving the object itself (since the callback will consult the weak pointer to see if it's still alive).

Paweł Hajdan, Jr.

unread,
Nov 12, 2012, 4:43:35 PM11/12/12
to you...@chromium.org, chromi...@chromium.org
1. src/base/timer.h may be very helpful for your use case.

2. What is your use case? What are you polling for? Can this be solved using events and not polling?

Paweł

--

Youngki Chang

unread,
Nov 12, 2012, 8:40:25 PM11/12/12
to Paweł Hajdan, Jr., chromi...@chromium.org
On Mon, Nov 12, 2012 at 4:43 PM, Paweł Hajdan, Jr. <phajd...@chromium.org> wrote:
1. src/base/timer.h may be very helpful for your use case.

Thanks for the pointer..!
 

2. What is your use case? What are you polling for? Can this be solved using events and not polling?

This could be off the point of this thread, but I'd like to listen to Bluetooth adapter state changes in Windows. There is a way to get the bluetooth adapter state info, but there is no notification API for bluetooth state change. So far I looked into Windows SDK plus internal framework such as network_change_notifier, ObjectWatcher, etc, and I couldn't find an API I could use in my use case. Hence we decided to go with polling.
Reply all
Reply to author
Forward
0 new messages