Skip to content

IAsyncOperation::GetResults returns an object that has been destructed. #1008

@seven-mile

Description

@seven-mile

Reproduced cpp/winrt version: 2.0.201201.7 and 2.0.210806.1

IAsyncOperation::GetResults returns an object that has been destructed. But in the same situation, I can use IAsyncOperation::get instead to get the correct object. Is it a bug? Or I missed something?

A minimal example to reproduce this issue:

winrt::HttpClient client;

winrt::IAsyncOperation<winrt::JsonObject> GetAsync(winrt::Uri rest_uri) {

  auto resp { co_await client.GetAsync(rest_uri) };
  if (!resp.IsSuccessStatusCode())
    winrt::throw_hresult(MAKE_HRESULT(SEVERITY_ERROR, FACILITY_HTTP, resp.StatusCode()));
      
  auto rest_content { co_await resp.Content().ReadAsStringAsync() };

  auto res = winrt::JsonObject::Parse(rest_content);
  // at least here, res is correct
  co_return res;
}

int main()
{
  winrt::init_apartment();

  auto get_op = GetAsync(winrt::Uri([ some URI returning json data ]));
  while (get_op.Status() == winrt::AsyncStatus::Started); // just wait

  if (get_op.Status() != winrt::AsyncStatus::Completed || !get_op.GetResults())
    return get_op.Status() == winrt::AsyncStatus::Canceled ? E_ABORT : get_op.ErrorCode().value;

  auto res = get_op.GetResults();
  // here res is "null" in debugger

  wprintf(L"%s\n", get_op.GetResults().ToString().c_str()); // failed: access violation (object already deleted)
  
  return 0;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions