From: Hendrik M. <qwi...@sn...> - 2008-01-30 12:27:06
|
Hi, I think my interface file is almost perfect now. However, I am unsure=20 whether the memory is managed properly. Can someone shed some light on=20 this? These are the relevant parts of my interface file: %module Mona %{ static char* printTreeNodeRecursively(mgTreeNode* self, int labelLength) = { \\ malloc a string and return it } %} struct mgTreeNode { /* tree node */ mA a; /* alphabet element */ struct mgTreeNode *left, *right; /* successors */ mgId id; /* state space id */ mgState state; /* automaton state */ }; %extend mgTreeNode { mgTreeNode(char *label, mgTreeNode *leftDaughter, mgTreeNode=20 *rightDaughter) { \\ malloc an mgTreeNode, as well as a, left and right, =09 \\ initialize and return it } char *toString(int labelLength) { return printTreeNodeRecursively($self, labelLength); } }; struct mgGta { /* Guided Tree Automaton */ unsigned numSS; /* number of state spaces */ mgStateSpace *stateSpace; /* array of state spaces */ unsigned numUnivs; /* number of universes */ mgUniverse *universe; /* array of universes */ unsigned numVars; /* number of free variables */ mgVariable *var; /* array of variables */ mgKind *final; /* initial state space state kinds */ unsigned numTypes; /* number of types (0: not using types) */ mgType *type; /* array of types (NULL if not using types) */ }; /* load GTA from text file */ /* This ensures Java takes care of garbage collecting automata created=20 by this method */ %newobject mgLoad; mgGta *mgLoad(const char *filename); %newobject printTreeNodeRecursively; %newobject toString; I only need few GTAs, so it wouldn=E2=80=99t be that big a disaster if so= mething=20 went wrong there, but I create masses of TreeNodes, so I want to make=20 sure they get reclaimed properly. I checked one tree constructed with=20 the nodes. The top-most node had swigCMemOwn true, but its left=20 daughter hadn=E2=80=99t. Now I don=E2=80=99t see the memory of the daugh= ters being=20 freed in the delete() function. Maybe I need %newobject mgTreeNode(char, mgTreeNode, mgTreeNode); or do I? Since that is converted to a Java constructor automatically, I = thought swig would be smart enought to know that it creates a new node=20 as well? Do I have to worry about the parts of the GTA not being reclaimed as=20 well? There is a function mgFree(mgGta*) in the library I am wrapping,=20 should I put that in the finalize() function? And write my own=20 freeTree(mgTreeNode*)? Cheers, H. --=20 Hendrik Maryns http://tcl.sfs.uni-tuebingen.de/~hendrik/ =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D http://aouw.org Ask smart questions, get good answers: http://www.catb.org/~esr/faqs/smart-questions.html |