内存泄漏从C ++调用.Net dll

时间:2016-08-22 02:40:38

标签: c++ memory-leaks

在下面的函数中从C ++调用.Net dll时出现内存泄漏。我认为它是使用SafeArray声明但我不知道除了SafeArrayDestroyDescriptor之外还有什么方法可以释放内存。我还缺少什么?

VARIANT_BOOL SendPack(IDotNetDll* wb, WW_POLL pl)
{
    HRESULT hr = 0;
    VARIANT_BOOL bretval;

    BYTE destination = pl.uDest;
    BYTE raw[2];
    raw[0] = pl.uAPI;
    raw[1] = pl.uOpcode;
    SAFEARRAY* bytes = NULL;
    hr = SafeArrayAllocDescriptor(1, &bytes);

    bytes->cbElements = sizeof(raw[0]);
    bytes->rgsabound[0].cElements = sizeof(raw);
    bytes->rgsabound[0].lLbound = 0;
    bytes->pvData = raw;
    bytes->fFeatures = FADF_AUTO | FADF_FIXEDSIZE;
    wb->SendMessage(destination,(BYTE)4, bytes, VARIANT_FALSE,
        200.0,&bretval);

    SafeArrayDestroyDescriptor(bytes);
    return bretval;
}

编辑: 我也尝试使用变体

这个方法
VARIANT_BOOL SendPacket(IDotNetDll* wb, WW_POLL pl)
{
    HRESULT hr = 0;
    VARIANT_BOOL bretval;
    _variant_t var;
    void * pArrayData = NULL;

    var.vt = VT_ARRAY | VT_UI1;
    SAFEARRAYBOUND rgsabound1[1];
    rgsabound1[0].cElements = 5;
    rgsabound1[0].lLbound = 0;

    var.parray = SafeArrayCreate(VT_UI1, 1, rgsabound1);

    BYTE destination = pl.uDest;
    SafeArrayAccessData(var.parray, &pArrayData);
    BYTE raw[5];
    raw[0] = pl.uAPI;
    raw[1] = pl.uOpcode;
    raw[2] = pl.uPayLoad[0];
    raw[3] = pl.uPayLoad[1];
    raw[4] = pl.uPayLoad[2];
    memcpy(pArrayData, raw, 5);
    SafeArrayUnaccessData(var.parray);

    //Send packet
    wb->SendMessage(destination,
        (BYTE)4, 
        var.parray,
        VARIANT_FALSE,
        200.0, &bretval);
    var.Clear();
    return bretval;
}

以下是VS 2015探查器profiler results

的内存使用情况

0 个答案:

没有答案