سؤال

Is it safe to assume that when the first then clause finishes, bitmapStream will be disposed because it goes out of scope at that point (thereby making it's ref count go to 0)?

BitmapImage^ bmp = ref new BitmapImage();
create_task(StorageFile::GetFileFromApplicationUriAsync(uri)).then([](StorageFile^ file)
{
    return file->OpenReadAsync();
}).then([bmp](IRandomAccessStream^ bitmapStream)
{
    return bmp->SetSourceAsync(bitmapStream);
}).then([bmp]()
{
    // Do some stuff with bmp here
});
هل كانت مفيدة؟

المحلول

Not really, StorageFile::OpenReadAsync() returns a IAsyncOperation<IRandomAccessStreamWithContentType>^ which is an asynchronous operation.

This operation creates and holds a reference to the IRandomAccessStream, when the operation is done, the PPL tasks get a reference to this stream through IAsyncOperation<TResult>::GetResults() and they hold the reference, at least, until the second lambda function is done (the lambda function in the second then()).

After that, if the BitmapImage holds another reference to the stream, then the stream won't be disposed for a "long" time.

If you would like to dig more into this topic, you may be able to create your own implementation of IRandomAccessStream interface and put a breakpoint in the Dispose method.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top