LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Generation of Function Panel creates code that calls frees memory it didn't allocate

Solved!
Go to solution

Generation of a Function Panel from our ActiveX component generates the code listed below.

 

My issue is that it is freeing memory that it didn't allocate - a bad programming practice. The user has to allocate a buffer for testFilePath but this method frees it.

 

A side effect of this is known issue:

 

http://digital.ni.com/public.nsf/allkb/205CA72511BC735C862570F80019660A?OpenDocument

 

 

John Barton

 

----------------------------------

 

HRESULT CVIFUNC XJIntegrationOCX__XJIntegrationSetTestSystem (CAObjHandle objectHandle,
                                                              ERRORINFO *errorInfo,
                                                              char **testFilePath,
                                                              VBOOL *arg2)
{
    HRESULT __result = S_OK;
    XJIntegrationOCX__XJIntegration_Interface * __vtblIFacePtr = 0;
    int __didAddRef;
    int __errorInfoPresent = 0;
    BSTR testFilePath__AutoType = 0;
    VBOOL arg2__Temp;

    __caErrChk (CA_CStringToBSTR (*testFilePath, &testFilePath__AutoType));

    __caErrChk (CA_GetInterfaceFromObjHandle (objectHandle,
                                              &XJIntegrationOCX_IID__XJIntegration,
                                              0, &__vtblIFacePtr, &__didAddRef));
    __caErrChk (__vtblIFacePtr->lpVtbl->SetTestSystem_ (__vtblIFacePtr,
                                                        &testFilePath__AutoType,
                                                        &arg2__Temp));

    if (testFilePath)
        {
        CA_FreeMemory (*testFilePath);
        *testFilePath = 0;
        }
    if (testFilePath)
        __caErrChk (CA_BSTRGetCString (testFilePath__AutoType, testFilePath));
    if (arg2)
        {
        *arg2 = arg2__Temp;
        }

Error:
    CA_FreeBSTR (testFilePath__AutoType);
    if (__vtblIFacePtr && __didAddRef)
        __vtblIFacePtr->lpVtbl->Release (__vtblIFacePtr);
    if (FAILED(__result))
        {
        if (testFilePath)
            {
            CA_FreeMemory (*testFilePath);
            *testFilePath = 0;
            }
        }
    CA_FillErrorInfoEx (objectHandle, &XJIntegrationOCX_IID__XJIntegration,
                        __result, errorInfo, &__errorInfoPresent);
    if (__errorInfoPresent)
        __result = DISP_E_EXCEPTION;
    return __result;
}

0 Kudos
Message 1 of 3
(2,882 Views)
Solution
Accepted by topic author jcbarton

Hi John,

 

Is this parameter supposed to be an input-output parameter? If so,  then what the code is doing seems correct to me. The function expects a string to be passed, and then returns a different string via the same parameter. In that case, by ActiveX rules, the method does free the original string after using it, and then allocates a new string. The only difference between a typical ActiveX interface and this CVI wrapper is that with a typical ActiveX interface it would be operating on BStr strings instead of C strings.

 

Because the function re-allocates the buffer after freeing the one that you pass in, it should not change the memory management of this buffer in the client code. The expectation is that you allocate the memory initially, then you call this function, and then free the memory sometime after this function returns. The fact that this function re-allocated the memory in the meanwhile should not affect this process.

 

If you're running into the invalid free issue, a couple of things to watch for are that: 1) when you allocate the memory initially, you are not using a different allocater, such as the C library's malloc, for example. If you need to allocate the memory explicitly, you should use CA_AllocMemory, which is what the ActiveX Library expects, and 2) that you do not have multiple references to this pointer that could potentially be freed multiple times.

 

Luis

0 Kudos
Message 2 of 3
(2,868 Views)

LuisG,

 

Apologies - I hadn't read the code closely enough to spot the line where it copies the buffer back:

 

 CA_BSTRGetCString (testFilePath__AutoType, testFilePath)

 

 

Your time has been much appreciated - and I've passed on your suggestion of using CA_AllocMemory() to our customer.

 

Regards

 

John

 

0 Kudos
Message 3 of 3
(2,863 Views)