Skip to content

AddHostObjectToScript throws exception when passing large SafeArray #784

Open
@ionpac

Description

@ionpac

Description

Hi,

I am trying to pass a file's content from C++ to a JavaScript App.

I am using the AddHostObjectToScript to transfer a SafeArray wrapped in a VARIANT.
(As described here: https://docs.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/icorewebview2?view=webview2-1.0.664.37#addhostobjecttoscript)

It works fine with small files but AddHostObjectToScript throws an exception for all filesizes larger than approximatley 50MB.

This is my code:

std::ifstream input(filePath, std::ios::binary);

if (!input.good())
    return E_FAIL;
                
SAFEARRAYBOUND saBound;
saBound.lLbound = 0;
saBound.cElements = fileSize;
SAFEARRAY* safeArray = SafeArrayCreate(VT_UI1, 1, &saBound);

if (safeArray) {
    HRESULT hr = SafeArrayLock(safeArray);
    if (SUCCEEDED(hr))
    {
        char* pData = static_cast<char*>(safeArray->pvData);

        input.read(pData, fileSize);
    }
    SafeArrayUnlock(safeArray);
} else {
    return E_FAIL;
}

input.close();

VARIANT remoteObjectAsVariant = {};
remoteObjectAsVariant.parray = safeArray;
remoteObjectAsVariant.vt = VT_ARRAY | VT_UI1;

HRESULT hr = S_OK;
try {
    hr = m_contentWebView->AddHostObjectToScript(L"myDataArray", &remoteObjectAsVariant);
} catch (...) {
    std::cout << "Caught exception";
}
hr = GetLastError();

This is the exception:

Exception thrown at 0x7725A892 in WebviewDesktopApp.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x00CFED54.
Debug Error!

GetLastError() returns 0x00000008 Not enough memory resources are available to process this command.

Version
SDK: 1.0.622.22
Runtime: Version 87.0.664.66 (Official build) (64-bit)
Framework: Win32
OS: Win10 19041

Repro Steps
I hope the code is enough to reproduce the issue.

AB#44875951

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpriority-lowWe have considered this issue and decided that we will not be able to address it in the near future.trackedWe are tracking this work internally.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions