gpt4 book ai didi

C++/CX - DataReader 越界异常

转载 作者:太空宇宙 更新时间:2023-11-04 13:26:38 27 4
gpt4 key购买 nike

我有以下代码可以打开一个文件,它大部分时间都可以工作一次。之后我抛出异常,我不知道问题出在哪里。我已经尝试寻找这个几天了,但没有找到。

String^ xmlFile = "Assets\\TheXmlFile.xml";
xml = ref new XmlDocument();
StorageFolder^ InstallationFolder = Windows::ApplicationModel::Package::Current->InstalledLocation;
task<StorageFile^>(
InstallationFolder->GetFileAsync(xmlFile)).then([this](StorageFile^ file) {
if (nullptr != file) {
task<Streams::IRandomAccessStream^>(file->OpenAsync(FileAccessMode::Read)).then([this](Streams::IRandomAccessStream^ stream)
{
IInputStream^ deInputStream = stream->GetInputStreamAt(0);
DataReader^ reader = ref new DataReader(deInputStream);
reader->InputStreamOptions = InputStreamOptions::Partial;
reader->LoadAsync(stream->Size);

strXml = reader->ReadString(stream->Size);

MessageDialog^ dlg = ref new MessageDialog(strXml);
dlg->ShowAsync();
});
}
});

错误是在这部分代码触发的:

strXml = reader->ReadString(stream->Size);

我收到以下错误:

First-chance exception at 0x751F5B68 in XmlProject.exe: Microsoft C++ exception: Platform::OutOfBoundsException ^ at memory location 0x02FCD634. HRESULT:0x8000000B The operation attempted to access data outside the valid range

WinRT 信息:操作试图访问有效范围之外的数据

就像我说的,第一次它能正常工作,但之后我得到了错误。我尝试分离数据读取器的流和缓冲区并尝试刷新流但没有结果。

最佳答案

我还在 Microsoft C++ 论坛上问过这个问题,并归功于 user “Viorel_”我设法让它工作。 Viorel 说了以下内容:

Since LoadAsync does not perform the operation immediately, you should probably add a corresponding “.then”. See some code: https://social.msdn.microsoft.com/Forums/windowsapps/en-US/94fa9636-5cc7-4089-8dcf-7aa8465b8047. This sample uses “create_task” and “then”: https://code.msdn.microsoft.com/vstudio/StreamSocket-Sample-8c573931/sourcecode (file Scenario1.xaml.cpp, for example).

我不得不将 task<Streams::IRandomAccessStream^> 中的内容分开并将其分解为单独的任务。

我重构了我的代码,现在我有以下内容:

String^ xmlFile = "Assets\\TheXmlFile.xml";
xml = ref new XmlDocument();
StorageFolder^ InstallationFolder = Windows::ApplicationModel::Package::Current->InstalledLocation;
task<StorageFile^>(
InstallationFolder->GetFileAsync(xmlFile)).then([this](StorageFile^ file) {
if (nullptr != file) {
task<Streams::IRandomAccessStream^>(file->OpenAsync(FileAccessMode::Read)).then([this](Streams::IRandomAccessStream^ stream)
{
IInputStream^ deInputStream = stream->GetInputStreamAt(0);
DataReader^ reader = ref new DataReader(deInputStream);
reader->InputStreamOptions = InputStreamOptions::Partial;
create_task(reader->LoadAsync(stream->Size)).then([reader, stream](unsigned int size){
strXml = reader->ReadString(stream->Size);
MessageDialog^ dlg = ref new MessageDialog(strXml);
dlg->ShowAsync();
});
});
}
});

关于C++/CX - DataReader 越界异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33165179/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com