From: Kris T. <kri...@gm...> - 2014-08-15 22:17:25
|
From: Vadim Zeitlin> Sent: 15 August 2014 22:56 > On Fri, 15 Aug 2014 19:16:28 +0100 Kris Thielemans An example could be > KT> > KT> // C++ > KT> class A {}; > KT> class B { > KT> public: > KT> B: a(new A) {} > KT> > KT> const A* getA() const { return a}; > VZ> This line wouldn't compile, I assume you meant "a.get()" here? Yes. Sorry > > KT> private: > KT> shared_ptr<A> a; > KT> }; > KT> > KT> // matlab > KT> b=B(); > KT> my_a=b.getA(); > KT> clear b; % remove 'b', so call its destructors > KT> %accessing 'my_a' now crashes as it points to a deleted object > VZ> This code is invalid in C++, so how do you expect it to work from any VZ> other language? The interface is just unsafe, to make it safe you'd need to VZ> return shared_ptr<A> from getA(), then things should just work, both in C++ VZ> and elsewhere. Hmmm. You're right! So... not a SWIG problem after all! [off topic: The reason I didn't want to return a shared_ptr in my library is that I didn't want people to be able to modify that shared_ptr, as then all hell could break loose. It looks like I really should return a shared_ptr<const A> for that, which I couldn't do when this library was designed 15 years ago...] Thanks a lot! Kris |