Skip to content

C# SGMatrix and SGVectorList typemaps leak memory #4752

Open
@ghost

Description

In the out typemaps for both SGMatrix and std::vector<SGVector>, memory is allocated using malloc, but it's not freed.

%typemap(out) shogun::SGVector<SGTYPE> {
int32_t i;
char *res;
int len = $1.vlen;
res = SG_MALLOC(char, sizeof(CTYPE) * len + sizeof(int32_t));
((int32_t*)res)[0] = len;
CTYPE * dst = (CTYPE *)(res + sizeof(int32_t));
for (i = 0; i < len; i++) {
dst[i ] = (CTYPE) $1.vector[i];
}
$result = (CTYPE *)res;
}

%fragment(SWIG_From_frag(std::vector<shogun::SGVector<SGTYPE>>), "header")
{
CTYPE* SWIG_From_dec(std::vector<shogun::SGVector<SGTYPE>>)
(const std::vector<shogun::SGVector<SGTYPE>>& strings)
{
int32_t rows = strings.size();
int32_t len = 0;
for(auto& str : strings)
len += str.vlen;
char *res = SG_MALLOC(char, len * sizeof(CTYPE) + (rows + 1) * sizeof(int32_t));
((int32_t *) res)[0] = rows;
for(int32_t i = 0; i < rows; i++)
((int32_t *) res)[i + 1] = strings[i].size();
CTYPE *data = (CTYPE *)((int32_t *) res + rows + 1);
for (auto& str : strings) {
sg_memcpy(data, str.vector, str.vlen * sizeof(SGTYPE));
data = data + str.size();
}
return (CTYPE *) res;
}
}

These allocated buffers are passed to C# code as IntPtr, and can't be deallocated from there.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions