From: the4thamigo_uk <and...@ba...> - 2012-12-20 12:57:46
|
Hi, I have a custom string class X, and functions that have input parameters passed as const X&. e.g. void f(const X& foo); I want to create a typemap such that a python string is converted to an instance of X by allocation on the stack e.g. %typemap(in) const X& { X x( PyString_AsString($input) ); $1 = x } However, it seems that swig generates code such that $1 is a pointer to X, so that in order to get swig to work I must allocate on x on the heap and provide a freearg typemap to deallocate it after the function call. I did try simply getting the address x e.g. %typemap(in) const X& { X x( PyString_AsString($input) ); $1 = &x } .... but this doesnt work because the cxx wrapper code places the typemap code within curly braces, so this code sets arg1 to be a dangling pointer... e.g. { X x( PyString_AsString(obj0) ); arg1 = &x; } f((X const &)*arg1); What is the solution to this as I dont want to be forced to always allocate on the heap for efficiency reasons. Thanks Andy -- View this message in context: http://old.nabble.com/const-reference-argument-but-construct-on-the-stack-tp34819182p34819182.html Sent from the swig-user mailing list archive at Nabble.com. |