简体   繁体   中英

SWIG memory issue of allocating array inside a struct

Suppose I have a C struct like this

struct foo {
    int sz;
    struct bar **arr;
};

In my SWIG file, I use "carrays.i" to give me control of the array inside this struct. Here I use

%module mdl
%include "carrays.i"
%array_functions(struct bar*, barArray)

Then I allocate memory for this array in python.

a = mdl.foo()
a.arr = mdl.new_barArray(sz)

My question is, should I free the memory of the allocated array by calling

mdl.delete_barArray(a.arr)

before I exit the current function, or SWIG will automatically do this for me so I don't need to worry about it?

You have to call delete when the array is no longer needed. If you don't do it before the foo object reference count goes to zero and is destroyed, you have a memory leak.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM