|
#include "gcptr.h"
#include <string>
#include <iostream>
//#define MANUAL_COLLECT
//#define COLLECT_ON_NEW
using namespace std;
struct b1
{
b1(const string& s) : name(s)
{
cout << "Creating b1(" << name << ")." << endl;
}
virtual ~b1()
{
cout << "Destroying b1(" << name << ")." << endl;
}
string name;
};
struct b2
{
b2(const string& s) : name(s)
{
cout << "Creating b2(" << name << ")." << endl;
}
virtual ~b2()
{
cout << "Destroying b2(" << name << ")." << endl;
}
string name;
};
struct d1 : public b1
{
d1(const string& s) : b1(s)
{
cout << "Creating d1(" << name << ")." << endl;
}
virtual ~d1()
{
cout << "Destroying d1(" << name << ")." << endl;
}
};
struct d2 : public b1, public b2
{
d2(const string& s) : b1(s), b2(s)
{
cout << "Creating d2(" << b1::name << ")." << endl;
}
virtual ~d2()
{
cout << "Destroying d2(" << b1::name << ")." << endl;
}
};
struct circ
{
circ(const string& s) : name(s)
{
cout << "Creating circ(" << name << ")." << endl;
}
~circ()
{
cout << "Destroying circ(" << name << ")." << endl;
}
gc_ptr<circ> ptr;
string name;
};
void test()
{
gc_ptr<b1> p1;
{
gc_ptr<d1> p2(new(gc) d1("first"));
p1 = p2;
gc_ptr<b1> p3(new(gc) d2("second"));
#ifdef MANUAL_COLLECT
gc_collect();
#endif
}
#ifdef MANUAL_COLLECT
gc_collect();
#endif
{
gc_ptr<circ> p4(new(gc) circ("root"));
{
gc_ptr<circ> p5(new(gc) circ("first"));
gc_ptr<circ> p6(new(gc) circ("second"));
gc_ptr<circ> p7(new(gc) circ("third"));
p4->ptr = p5;
p5->ptr = p6;
p6->ptr = p7;
p7->ptr = p5;
#ifdef MANUAL_COLLECT
gc_collect();
#endif
}
#ifdef MANUAL_COLLECT
gc_collect();
#endif
}
#ifdef MANUAL_COLLECT
gc_collect();
#endif
}
int main()
{
#ifdef COLLECT_ON_NEW
gc_set_threshold(0);
#endif
test();
gc_collect();
return 0;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Windows developer with 10+ years experience working in the banking industry.