From: Bo P. <be...@gm...> - 2006-09-25 19:02:21
|
On 9/25/06, Sohail Somani <s.s...@fi...> wrote: > > def new_myClass(self, rate=[], *args, **kwargs): > > # allow input of both int, float or vector, and prepare parameter r > > if type(rate) in [types.IntType, types.FloatType]: > > r = [rate] > > else: > > r = rate > > # create the object using the C++ constructor, and set to this. > > _swig_setattr(self, myClass, 'this', > > _myCppModule.new_myClass(rate=r, *args, **kwargs)) > > _swig_setattr(self, myClass, 'thisown', 1) I read the generated .py file again and find that _swig_setattr seems to be obsolete. I replace the above two lines with _myCppModule.myClass_swiginit(self, _myCppModule.new_myClass(rate=r, *args, **kwargs)) and the memory leak seems to be gone. (Still testing.) > Can I suggest another way: > > In your C++ class, have a typedef that says IntVectorOrNumber then > provide a typemap. I actually though of this but my approach seems to be more flexible in that 1. The users can pass different types of parameter, such as myClass(1), myClass([1,2]) in a more intuitive way (instead of myClass([1]). 2. I can do extensively error checking before passing the parameters to C++. (typemap can not do this.) 3. I can add more parameters if needed, for example, defining myClass(rate=1) as myClass(rates=[1]) for myClass(rates=vector<int>). All these, if done in C++, will be cumbersome and may face the difficulties in function overloading etc. Bo |