BindOnce
Posted:
Tue Nov 23, 2021 7:56 am
by arczi
Hi, how can I call method in TID_UI?
I'm upgrading CEF version and in old code:
CefPostTask(TID_UI, base::Bind(&setZoomLevel, browser, delta));
was working well now because Bind has been removed I tried to use BindOnce instead but something is wrong.
CefPostTask(TID_UI, base::BindOnce(&setZoomLevel, browser, delta));
That's the declaration of a zoom method
void zoomLevel(CefRefPtr<CefBrowser> browser, double *level)
How am I supposed to call this method in UI thread without declaring another class which inherit from CefTask?
Re: BindOnce
Posted:
Tue Nov 23, 2021 10:14 am
by alexandermol
Are you getting any compile errors?
What is zoomLevel a member of?
I suspect you need
CefPostTask(TID_UI, base::BindOnce(&setZoomLevel, this, browser, delta));
Re: BindOnce
Posted:
Tue Nov 23, 2021 10:46 am
by magreenblatt
arczi wrote:That's the declaration of a zoom method
void zoomLevel(CefRefPtr<CefBrowser> browser, double *level)
You can't use out pointers with bound methods. I suggest you change your code so that the result (level) is consumed on the UI thread.
Re: BindOnce
Posted:
Tue Nov 23, 2021 11:58 am
by alexandermol
What do you mean? I am sending a void* as parameter and calling it in a bound function, it works fine?
- Code: Select all
void WebSocketHandler::onConnect(seasocks::WebSocket *connection) {
....
CefPostTask(TID_UI,
base::BindOnce(base::IgnoreResult(&BrowserApp::CreateBrowser),
base::Unretained(m_app), connection));
....
}
Re: BindOnce
Posted:
Tue Nov 23, 2021 12:20 pm
by magreenblatt
alexandermol wrote:What do you mean? I am sending a void* as parameter and calling it in a bound function, it works fine?
Think about the function signature that was posted:
- Code: Select all
void zoomLevel(CefRefPtr<CefBrowser> browser, double *level)
What happens with |level| when zoomLevel is executed on a different thread? Is |level| likely to still reference a valid memory address at that time? Of course it might if |level| is heap allocated or if you're somehow blocking the calling thread on zoomLevel completion, but neither of those are good design approaches.
Re: BindOnce
Posted:
Fri Nov 26, 2021 2:53 am
by arczi
|level| is actually heap allocated and I'm using shared_ptr so I don't worry about memory leak or dangling pointer. That was problem with implementation and now it works fine.
Re: BindOnce
Posted:
Thu Oct 26, 2023 12:03 am
by tapineb371
arczi wrote:|level| is actually heap allocated and I'm using shared_ptr so I don't worry about memory leak or dangling pointer. That was problem with implementation and now it works fine.
Can you elaborate on the solution @arczi?