Fix #3024875 - shared_ptr of classes with non-public destructors. This also fixes the 'unref' feature when used on classes with non-public destructors.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@12155 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
d0d40db326
commit
cca92f4188
6 changed files with 92 additions and 1 deletions
|
|
@ -13,5 +13,8 @@ public class runme
|
|||
int sum = li_boost_shared_ptr_bits.sum(v);
|
||||
if (sum != 66)
|
||||
throw new ApplicationException("sum is wrong");
|
||||
|
||||
HiddenDestructor hidden = HiddenDestructor.create();
|
||||
hidden.Dispose();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,5 +20,8 @@ public class li_boost_shared_ptr_bits_runme {
|
|||
int sum = li_boost_shared_ptr_bits.sum(v);
|
||||
if (sum != 66)
|
||||
throw new RuntimeException("sum is wrong");
|
||||
|
||||
HiddenDestructor hidden = HiddenDestructor.create();
|
||||
hidden.delete();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -48,3 +48,81 @@ int sum(std::vector< boost::shared_ptr<IntHolder> > v) {
|
|||
|
||||
%template(VectorIntHolder) std::vector< boost::shared_ptr<IntHolder> >;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////
|
||||
// Test non public destructor - was leading to memory leaks as the destructor was not wrapped
|
||||
// Bug 3024875
|
||||
/////////////////////////////////////////////////
|
||||
|
||||
%shared_ptr(HiddenDestructor)
|
||||
|
||||
%inline %{
|
||||
class HiddenDestructor;
|
||||
typedef boost::shared_ptr< HiddenDestructor > FooPtr;
|
||||
|
||||
class HiddenDestructor {
|
||||
public:
|
||||
static FooPtr create();
|
||||
virtual void doit();
|
||||
|
||||
protected:
|
||||
HiddenDestructor();
|
||||
static void Foo_body( FooPtr self );
|
||||
virtual ~HiddenDestructor();
|
||||
private:
|
||||
HiddenDestructor( const HiddenDestructor& );
|
||||
class Impl;
|
||||
Impl* impl_;
|
||||
|
||||
class FooDeleter {
|
||||
public:
|
||||
void operator()(HiddenDestructor* hidden) {
|
||||
delete hidden;
|
||||
}
|
||||
};
|
||||
};
|
||||
%}
|
||||
|
||||
%{
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
/* Impl would generally hold a weak_ptr to HiddenDestructor a.s.o, but this stripped down example should suffice */
|
||||
class HiddenDestructor::Impl {
|
||||
public:
|
||||
int mymember;
|
||||
};
|
||||
|
||||
FooPtr HiddenDestructor::create()
|
||||
{
|
||||
FooPtr hidden( new HiddenDestructor(), HiddenDestructor::FooDeleter() );
|
||||
Foo_body( hidden );
|
||||
return hidden;
|
||||
}
|
||||
|
||||
void HiddenDestructor::doit()
|
||||
{
|
||||
// whatever
|
||||
}
|
||||
|
||||
HiddenDestructor::HiddenDestructor()
|
||||
{
|
||||
// cout << "HiddenDestructor::HiddenDestructor()" << endl;
|
||||
// always empty
|
||||
}
|
||||
|
||||
void HiddenDestructor::Foo_body( FooPtr self )
|
||||
{
|
||||
// init self as you would do in ctor
|
||||
self->impl_ = new Impl();
|
||||
}
|
||||
|
||||
HiddenDestructor::~HiddenDestructor()
|
||||
{
|
||||
// cout << "HiddenDestructor::~HiddenDestructor()" << endl;
|
||||
// destruct (e.g. delete Pimpl object)
|
||||
delete impl_;
|
||||
}
|
||||
%}
|
||||
////////////////////////////
|
||||
|
||||
|
|
|
|||
|
|
@ -29,3 +29,6 @@ sum = sum(v)
|
|||
if sum != 66:
|
||||
raise "sum is wrong"
|
||||
|
||||
################################
|
||||
p = HiddenDestructor.create()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue