From: Raoul G. <rao...@ya...> - 2006-01-27 10:22:27
|
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? -- Raoul Gough. ___________________________________________________________ Yahoo! Messenger - NEW crystal clear PC to PC calling worldwide with voicemail http://uk.messenger.yahoo.com |