MicrosoftEdge / MicrosoftEdge/WebView2Feedback

AddHostObjectToScript throws exception when passing large SafeArray

Open
#784 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug priority-low tracked
Dominant language
PowerShell
Stars
526
Forks
67
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the AddHostObjectToScript call and the SafeArray construction shown in the issue, then reproduce the failure with files above and below the reported size. Check the WebView2 host-object documentation and determine whether large SafeArrays are expected to work or have a documented limit; done means the behavior or limitation is confirmed with a reproducible result.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
api, desktop
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.