0

我正在使用 Visual Studio 17 v15.0 和 Win 10 周年更新 SDK。

我使用、或构建以下代码(基本上是 github repo 中的示例)。它编译没有错误。cl /EHsc /O2 /DUNICODE /bigobj /await /std:c++latest/MTMD

如果我在当前目录中不存在“message.png”时运行,则会抛出异常,并使用 printf 捕获并报告,然后退出而不会崩溃。

如果我在当前目录中存在“message.png”时运行,“Hello World ” 将被打印,然后无缘无故崩溃。

奇怪的是,如果我在 GDB 调试器中运行它,GDB 总是说程序正常退出(实际上没有发生崩溃)。

GDB 输出:

[New Thread 1364.0x2324]
[New Thread 1364.0x624]
[New Thread 1364.0x12cc]
[New Thread 1364.0x58c]
[New Thread 1364.0x1134]
[New Thread 1364.0x10d8]
[New Thread 1364.0x18a8]
[New Thread 1364.0x1794]
[New Thread 1364.0x20e8]
[New Thread 1364.0x2204]
[New Thread 1364.0x1030]
[New Thread 1364.0x1474]
Hello world!
[Thread 1364.0x10d8 exited with code 0]
[Thread 1364.0x624 exited with code 0]
[Thread 1364.0x20e8 exited with code 0]
[Thread 1364.0x1794 exited with code 0]
[Thread 1364.0x18a8 exited with code 0]
[Thread 1364.0x58c exited with code 0]
[Thread 1364.0x1134 exited with code 0]
[Thread 1364.0x12cc exited with code 0]
[Thread 1364.0x8d0 exited with code 0]
[Thread 1364.0x2324 exited with code 0]
[Thread 1364.0x1b38 exited with code 0]
[Thread 1364.0x2204 exited with code 0]
[Thread 1364.0x1030 exited with code 0]
[Thread 1364.0x1474 exited with code 0]
[Inferior 1 (process 1364) exited normally]

代码:

#pragma comment(lib, "windowsapp") 
#pragma comment(lib, "pathcch")

#include <winrt/Windows.Storage.Streams.h>
#include <winrt/Windows.Graphics.Imaging.h>
#include <winrt/Windows.Media.Ocr.h>
#include <winrt/Windows.Networking.Sockets.h>

#include <pathcch.h>

using namespace winrt;
using namespace std::chrono;

using namespace Windows::Foundation;
using namespace Windows::Storage;
using namespace Windows::Storage::Streams;
using namespace Windows::Graphics::Imaging;
using namespace Windows::Media::Ocr;

hstring MessagePath()
{
    wchar_t buffer[1024]{};
    GetCurrentDirectory(_countof(buffer), buffer);
    check_hresult(PathCchAppendEx(buffer, _countof(buffer), L"message.png", PATHCCH_ALLOW_LONG_PATHS));
    return buffer;
}

IAsyncOperation<hstring> AsyncSample()
{
    StorageFile file = co_await StorageFile::GetFileFromPathAsync(MessagePath());
    IRandomAccessStream stream = co_await file.OpenAsync(FileAccessMode::Read);

    BitmapDecoder decoder = co_await BitmapDecoder::CreateAsync(stream);
    SoftwareBitmap bitmap = co_await decoder.GetSoftwareBitmapAsync();

    OcrEngine engine = OcrEngine::TryCreateFromUserProfileLanguages();
    OcrResult result = co_await engine.RecognizeAsync(bitmap);
    return result.Text();
}

int main()
{
    init_apartment();

    try
    {
        printf("%ls\n", AsyncSample().get().c_str());
    }
    catch (hresult_error const & e)
    {
        printf("hresult_error: (0x%8X) %ls\n", e.code(), e.message().c_str());
    }

    return 0;
}
4

1 回答 1

0

原来hstring返回的AsyncSample().get()不是空终止,所以printf崩溃。

try
{
    auto ans = AsyncSample().get();
    printf("[%u]: ", ans.size());
    auto s = ans.c_str();
    for (uint32_t i = 0; i < ans.size(); i++) {
        printf("%lc", s[i]);
    }
    putchar('\n');
}
于 2017-05-13T03:15:41.910 回答