Extend life time of wxWebRequest while it is in process

We already did it just before processing the state change event, but
this was too late, as the object could have been already deleted by then
and this actually happened with the example from wxWebRequest
documentation.

Do it earlier now, as soon as the request becomes active, which normally
happens when Start() is called, and keep the reference until the event
is processed after the request reaches one of the final states
(completed, failed or cancelled).

Add a unit test checking that deleting the wxWebRequest object doesn't
prevent the request from running to the completion any more.
This commit is contained in:
Vadim Zeitlin
2021-03-24 16:04:54 +01:00
parent d7235ebb05
commit 360268ee25
3 changed files with 56 additions and 15 deletions

View File

@@ -104,7 +104,7 @@ public:
break;
case wxWebRequest::State_Completed:
if ( request.GetStorage() == wxWebRequest::Storage_File )
if ( request.IsOk() && request.GetStorage() == wxWebRequest::Storage_File )
{
wxFileName fn(evt.GetDataFile());
CHECK( fn.GetSize() == expectedFileSize );
@@ -438,6 +438,26 @@ TEST_CASE_METHOD(RequestFixture,
REQUIRE( request.GetState() == wxWebRequest::State_Cancelled );
}
TEST_CASE_METHOD(RequestFixture,
"WebRequest::Destroy", "[net][webrequest]")
{
if ( !InitBaseURL() )
return;
Create("/base64/U3RpbGwgYWxpdmUh");
request.Start();
// Destroy the original request: this shouldn't prevent it from running to
// the completion!
request = wxWebRequest();
RunLoopWithTimeout();
CHECK( stateFromEvent == wxWebRequest::State_Completed );
CHECK( statusFromEvent == 200 );
CHECK( responseStringFromEvent == "Still alive!" );
}
// This test is not run by default and has to be explicitly selected to run.
TEST_CASE_METHOD(RequestFixture,
"WebRequest::Manual", "[.]")