In my C code, I have the following structure :
typedef struct my_structure{
char* str1;
char* str2;
}MyStruct;
And a function that returns a MyStruct pointer :
MyStruct* foo();
Inside foo, I have allocated memory for MyStruct , str1 and str2, as follows:
MyStruct* obj = malloc(sizeof(MyStruct));
obj.str1 = malloc(256);
obj.str2 = malloc(256);
I want to call foo from python, java, C# and PHP and I don't want to have any memory leak in this process.
I am not sure if writing:
%newobject foo;
MyStruct* foo();
guarantees that the garbage collector will free memory for both the structure and the strings inside it.
I didn't want to obligate the caller to explicit free memory for str1 and str2 as I was looking for an automatic way of freeing memory. Is this possible?
Do I have to use "newfree" typemap in this case?
I would greatly appreciate if you could provide me an example showing the best way to accomplish this.
Thanks!