Question on WeakPtr

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Question on WeakPtr

Postby vim » Thu Jul 20, 2017 8:55 am

Related to below the example, in cef_weak_ptr.h, of WeakPtr use,
controller is a WeakPtr, is it possible that controller_ is invalidate on point 2., and if not why?

//1. if (controller_)
//2.
// 3. controller_->WorkComplete(result);


class Controller {
// public:
// Controller() : weak_factory_(this) {}
// void SpawnWorker() { Worker::StartNew(weak_factory_.GetWeakPtr()); }
// void WorkComplete(const Result& result) { ... }
// private:
// // Member variables should appear before the WeakPtrFactory, to ensure
// // that any WeakPtrs to Controller are invalidated before its members
// // variable's destructors are executed, rendering them invalid.
// WeakPtrFactory<Controller> weak_factory_;
// };
//
// class Worker {
// public:
// static void StartNew(const WeakPtr<Controller>& controller) {
// Worker* worker = new Worker(controller);
// // Kick off asynchronous processing...
// }
// private:
// Worker(const WeakPtr<Controller>& controller)
// : controller_(controller) {}
// void DidCompleteAsynchronousProcessing(const Result& result) {
// if (controller_)
// controller_->WorkComplete(result);
// }
// WeakPtr<Controller> controller_;
// };
vim
Newbie
 
Posts: 3
Joined: Thu Jul 20, 2017 8:42 am

Re: Question on WeakPtr

Postby magreenblatt » Thu Jul 20, 2017 11:08 am

vim wrote:Related to below the example, in cef_weak_ptr.h, of WeakPtr use,
controller is a WeakPtr, is it possible that controller_ is invalidate on point 2., and if not why?

The code runs on a single thread so there is no opportunity for controller_ to be invalidated at point 2.
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: Question on WeakPtr

Postby vim » Fri Jul 21, 2017 2:06 am

The error is this:


done[0721/085109.809:FATAL:cef_weak_ptr.cc(20)] Check failed: thread_checker_.CalledOnValidThread() || HasOneRef(). WeakPtrs must be invalidated on the same thread.



with this is the code:
class Controller
{
public:
Controller() : mark(1),mark2(2),state(0),weak_factory_(this) {}
void SpawnWorker();
void WorkComplete(const int& result)
{

std::cout << result;
if (!mark.create)
std::cout << "ERROR: mark has been destruct";
if (!mark2.create)
std::cout << "ERROR: mark has been destruct";
}
~Controller()
{
state = 1;
std::cout << "done";
}
private:
Mark mark;
Mark mark2;
int state;
// Member variables should appear before the WeakPtrFactory, to ensure
// that any WeakPtrs to Controller are invalidated before its members
// variable's destructors are executed, rendering them invalid.
base::WeakPtrFactory<Controller> weak_factory_;



};

class Worker : public virtual CefBaseRefCounted
{
public:
static void StartNew(const base::WeakPtr<Controller>& controller)
{
Worker* worker = new Worker(controller);

int result = 12345;
base::Closure cb = base::Bind(&Worker::DidCompleteAsynchronousProcessing, worker, result);
CefRefPtr<CefTask> task = CefCreateClosureTask(cb);
worker->PostTask(task);

// Kick off asynchronous processing...
}

void PostTask(CefRefPtr<CefTask> task)
{
workerThread_->GetTaskRunner()->PostTask(task);
}
private:
Worker(const base::WeakPtr<Controller>& controller)
: controller_(controller)
{
workerThread_ = CefThread::CreateThread("a");
}
void DidCompleteAsynchronousProcessing(const int result)
{
if (controller_)
{
sleep(20000); // 20 sec, here controller is destruct
controller_->WorkComplete(result);
}
}

CefRefPtr<CefThread> workerThread_;
base::WeakPtr<Controller> controller_;
IMPLEMENT_REFCOUNTING(Worker);
DISALLOW_COPY_AND_ASSIGN(Worker);
};

void Controller::SpawnWorker() { Worker::StartNew(weak_factory_.GetWeakPtr()); }

main()
{
Controller *controllerp = new Controller();
controllerp->SpawnWorker();
sleep(2000); // 2sec
delete controllerp;
sleep(30000);
}
vim
Newbie
 
Posts: 3
Joined: Thu Jul 20, 2017 8:42 am

Re: Question on WeakPtr

Postby magreenblatt » Fri Jul 21, 2017 1:13 pm

WeakPtr needs to invalidated/dereferenced from a single thread. It is not safe to access on multiple threads.
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: Question on WeakPtr

Postby vim » Tue Aug 01, 2017 10:56 am

Thank you
vim
Newbie
 
Posts: 3
Joined: Thu Jul 20, 2017 8:42 am


Return to Support Forum

Who is online

Users browsing this forum: Google [Bot] and 35 guests