wxWebRequest reference counting

128 views
Skip to first unread message

Tobias T

unread,
Mar 8, 2021, 7:11:22 AM3/8/21
to wx-dev
I've recently stumbled over the following:

The wxWebRequest sample snippet we provide in the documentation
has a little issue: If you use a local variable only for the wxWebRequest there
is no reference left and the callback will never happen because it get's freed at the end
of the code containing the local variable.

I think it would make sense to support this coding pattern as I could think of a few instances where it might make sense.

Maybe a possible solution would be to add a reference when wxWebRequest::Start() is called and release it when it's finished?

Vadim Zeitlin

unread,
Mar 9, 2021, 2:47:23 PM3/9/21
to wx-...@googlegroups.com
On Mon, 8 Mar 2021 04:11:22 -0800 (PST) Tobias T wrote:

TT> The wxWebRequest sample snippet we provide in the documentation
TT> https://docs.wxwidgets.org/trunk/classwx_web_request.html
TT> has a little issue: If you use a local variable only for the wxWebRequest
TT> there
TT> is no reference left and the callback will never happen because it get's
TT> freed at the end of the code containing the local variable.

Oops. Thanks for pointing this out.

TT> I think it would make sense to support this coding pattern as I could think
TT> of a few instances where it might make sense.

We could capture the request by copy in the lambda to fix the example, but
this doesn't look like a good solution.

TT> Maybe a possible solution would be to add a reference when
TT> wxWebRequest::Start() is called and release it when it's finished?

I think this is a good idea. Releasing it after the request completion
shouldn't be surprising because either there are other references to the
object and in this case it doesn't change the observable behaviour, or
there are no other references and then there is no way to do anything from
this object any more. I.e. I don't see how could it be a problem.

Do you already see how this would be done? I.e. when/where exactly should
Release() be called?

Thanks,
VZ

Vadim Zeitlin

unread,
Mar 24, 2021, 1:14:25 PM3/24/21
to wx-...@googlegroups.com
On Mon, 8 Mar 2021 04:11:22 -0800 (PST) Tobias T wrote:

TT> Maybe a possible solution would be to add a reference when
TT> wxWebRequest::Start() is called and release it when it's finished?

I've done something like this in

https://github.com/wxWidgets/wxWidgets/pull/2292

now, please let me know if anybody has any comments about it.

TIA!
VZ
Reply all
Reply to author
Forward
0 new messages