SWIG homepage says shared_ptr is specially handled, but weak_ptr not. Does it means weak_ptr supporting has some bug/issue in SWIG? If it's ok to use, how to use it? Can anybody please give a sample .i code? Thanks very much.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
weak_ptr
doesn't need any special support in SWIG to use.The operations that need special support in SWIG for
shared_ptr
are dereferencing and passing into functions. This is because you never directly dereference or create aweak_ptr
. Instead when you're using it normally in C++ you would call thelock()
member function to upgrade it to a full, retainedshared_ptr
or one of the other functions to query its state.So all you need to do in practice therefore is wrap
weak_ptr
like any other template and use the existingshared_ptr
support in conjunction. For example:This can be exercised with some Java:
When run this gives:
(Notice here that
System.gc()
has been completely ignored by my runtime, so the attempt to lock again does actually succeed)But it also works with the same .i file for the following Python:
And when run gives:
Where the reference counting instead of GC does result in the call to
lock()
failing after the lastshared_ptr
has no more references.