【问题标题】:C++/CX - DataReader out of bounds exceptionC++/CX - DataReader 越界异常
【发布时间】:2015-10-19 02:05:44
【问题描述】:

我有以下代码可以打开一个文件,并且它大部分时间都可以运行一次。之后我抛出异常,我不知道问题隐藏在哪里。我已经尝试了几天,但没有运气。

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);

我收到以下错误:

XmlProject.exe 中 0x751F5B68 处的第一次机会异常:Microsoft C++ 异常:Platform::OutOfBoundsException ^ 在内存位置 0x02FCD634。 HRESULT:0x8000000B 操作试图访问有效范围之外的数据

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

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

【问题讨论】:

    标签: c++ file exception-handling c++-cx hresult


    【解决方案1】:

    我还在 Microsoft C++ 论坛上问过这个问题,并感谢user“Viorel_”我设法让它工作。 Viorel 说:

    由于 LoadAsync 不会立即执行操作,您可能应该添加一个相应的“.then”。查看一些代码:https://social.msdn.microsoft.com/Forums/windowsapps/en-US/94fa9636-5cc7-4089-8dcf-7aa8465b8047。此示例使用“create_task”和“then”:https://code.msdn.microsoft.com/vstudio/StreamSocket-Sample-8c573931/sourcecode(例如文件 Scenario1.xaml.cpp)。

    我不得不将task&lt;Streams::IRandomAccessStream^&gt; 中的内容分开并将其拆分为单独的任务。

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

    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();
                });             
            });
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-07
      • 2013-11-28
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多