|
From: Marcelo M. <mm...@ac...> - 2006-01-27 16:47:48
|
Check the docs, you can use the %pythonappend or %pythoncode directives
to add extra code to the proxy methods.
Marcelo
Louis Pecora wrote:
> Raoul Gough wrote:
>
>> I have a low-level C++ tracing class that I'm trying to wrap for
>> Python, which has an interface like this:
>>
>> struct TraceObject {
>> TraceObject(char const *functionName);
>> // ...
>> private:
>> char const *m_functionName;
>> };
>>
>> The problem is that it assumes the string passed to the constructor has
>> static storage duration, e.g.
>>
>> void foo() {
>> TraceObject e("foo"); // OK - "foo" string lives forever
>>
>> // ...
>> }
>>
>> However, when I construct one of these from Python, it gets passed a
>> Python string which nobody saves a reference to. The string then gets
>> destroyed, leaving a dangling pointer in the C++ object:
>>
>> e.g.
>>
>>
>>>>> e = TraceObject("foo") # PROBLEM
>>>>>
>>>>
>>
>> I guess what I would like is for the Python wrapper object to save a
>> reference to the string argument passed to its __init__ function.
>> Something along the lines of this:
>>
>> class TraceObject(_object):
>> def __init__(self, *args):
>> _swig_setattr(self, TraceObject, 'this',
>> _TraceObject.new_TraceObject(*args))
>> _swig_setattr(self, TraceObject, 'thisown', 1)
>> self.saved_arguments = args
>>
>> That way the Python shadow class will save a reference to the Python
>> string until it gets deleted itself. I don't see how to get swig to
>> generate this kind of code, though. Can anyone suggest how to deal with
>> this?
>>
>>
>
> I'm sorry, but I must be missing something. Why not create your own
> storage for the input string and copy to that? I assume when the
> calling class is destroyed the trace class will be also, so if you
> write the destructor right the memory will only disappear then.
>
> That's all in the C++ end and seems to me it would work. So what am I
> missing with the Python end?
>
|