8

无法在此代码中找到发生内存泄漏的位置。

基本上我想为一个返回二维数组的 C 函数编写一个 XS 包装器。

C函数:

int CW_returnArray(double** arrayDouble, int* count)
{
    int number = 10;
    int index, index1;
    for(index = 0; index < number; index++)
    {
        for(index1 = 0; index1 < 10000; index1++)
        {
            arrayDouble[index][index1] = 12.51;
        }

        count[index] = 10000; 
    }
    return number;
}

array -> output param to hold the two dimensional array
count -> output param to hold the number of element in each 1D array

XS 包装:

void
returnArray()
    PPCODE:
    {
    /** variable declaration **/
    double** array;
    int i = 0, j=0,  status;
    int* count;
    int totalArrays;

    SV** SVArrays;     // to hold the references of 1D arrays
    SV** SVtempArray;  // temporary array to hold the elements of 1D array

    /** allocate memory for C-type variables **/
    New(0, array, 10, double*);

    for(i = 0; i<10;i++)
    {
        New(0, array[i], 10000, double);
    }

    New(0, count, 10, int);

    /** call C function **/
    status = CW_returnArray(array, count); 

    /** check the status and retrieve the array to store it in stack **/
    if(status > 0)
    {
        totalArrays = status;

        New(0, SVArrays, totalArrays, SV*);
        for(i = 0; i<totalArrays; i++)
        {
            /** allocate memory for temporary SV array **/
            New(0, SVtempArray, count[i], SV*);
            for(j = 0; j<count[i]; j++)
            {
                SVtempArray[j] = newSVnv(array[i][j]);
            }

            /** Make an array (AV) out of temporary SV array and store the reference in SVArrays **/
            SVArrays[i] = newRV_noinc((SV*) av_make(count[i], SVtempArray));

            /** free the memory allocated for temp SV array **/
            for(j = 0; j<count[i]; j++)
            {
                sv_free(SVtempArray[j]);
            }               

            Safefree(SVtempArray); SVtempArray = NULL;
        }
    }
    else
    {
        totalArrays = 0;

    }

    /** push the return values to stack **/
    EXTEND(SP, 2);
    PUSHs(sv_2mortal(newSViv(status)));
    PUSHs(sv_2mortal(newRV_noinc((SV*) av_make(totalArrays, SVArrays))));

    /** clean up allocated memory for SV "array of array" , if needed **/
    if(totalArrays > 0)
    {
        Safefree(SVArrays); SVArrays = NULL;
    }

    /** clean up allocated memory for C-type variables **/
    for(i = 0; i<10;i++)
    {
        Safefree(array[i]);
    }       
    Safefree(array); array = NULL;
    Safefree(count); count = NULL;
}

从 XS 返回一个“数组数组”。

在 Perl 脚本中进行测试:

for(1..100)
{
    my ($status, $arrayref) = returnArray();
    undef $status;
    $arrayref = [];
    system('pause');
}

每次returnArray()调用该函数时,Perl 进程的 Commit 大小都会增加。但我希望$arrayref变量每次都应该被垃圾收集,并且内存使用量不应该增加。

我希望,我正在释放 XS 中所有分配的内存。但是仍然存在内存泄漏。这个内存泄漏的 XS 代码有什么问题?

4

1 回答 1

7

好吧,“创建一个模板数组,做av_make(),然后释放模板”的模式不是很好——你最好通过简单地创建你的数组newAV()av_extend()将它设置为正确的大小,然后av_store(newSVnv(...))为每个元素做. 这可以让您完全避免中间SVtempArray分配。

不过,你问的不是这个。我认为你的问题是你Safefree(SVArrays)没有先sv_free()ing 每个元素。由于av_make() 复制了源数组的内容,AFAICT 你正在泄漏由创建的引用

SVArrays[i] = newRV_noinc((SV*) av_make(count[i], SVtempArray));

您需要迭代 SVArrays 并sv_free()Safefree(SVArrays).

于 2013-08-19T03:08:00.473 回答