开发者

Allocator for [unique] pointers

开发者 https://www.devze.com 2023-03-21 03:00 出处:网络
I\'m slightly puzzled by the lack of documentation on the issue, so I may be completely off track here:

I'm slightly puzzled by the lack of documentation on the issue, so I may be completely off track here:

When I allocate memory in order to return an object through an unique pointer whose value I have modified, what allocater should I use?

The documentation says that I can provide MIDL_user_allocate() and MIDL_user_free() and the stub will use these -- however that does not make sense in CLSCTX_INPROC_SERVER, as the calling object would need to use (and hence resolve) my allocater.

So, how should I allocate memory here, so that the stub code can properly free 开发者_Go百科the list if the DLL is loaded into SVCHOST, and applications can still use the DLL directly if they so desire.

idl:

HRESULT GetItems([out] DWORD *count, [out, size_is(,count)] ITEM **items);

cpp:

HRESULT STDMETHODCALLTYPE impl::GetBuffer(DWORD *count, ITEM **items)
{
    *count = 0;
    *items = reinterpret_cast<ITEM *>(/* ??? */);
    if(!*items)
        return E_OUTOFMEMORY;
    *count = 5;
    /* fill in items */
    return S_OK;
}


From here:

Out-parameters must be allocated by the one called; they are freed by the caller using the standard COM task memory allocator.

where COM task memory allocator is either the set of IMalloc methods or the set of CoTaskMemAlloc()/CoTaskMemRealloc()/CoTaskMemFree() functions that provide the same functionality.

The midl_user-*() functions you mention are used for RPC memory management. You need them in case you deal with RPC interfaces, not COM interfaces.

0

精彩评论

暂无评论...
验证码 换一张
取 消