Thanks for that!
In fact I'm a little bit of a C++ newbie as well. Anyway, to make things clear to me I tried the following test, created a header
Lang.h
#pragma once
ref class Lang
{
public:
Lang(int n);
~Lang();
void Poo();
private:
Lang^ sub;
int n0;
};
Lang::Lang(int n)
: n0(n), sub(nullptr)
{
if(n>0)
sub = ref new Lang(n - 1);
Platform::Console::WriteLine("Lang(" + n0.ToString() + ")");
}
Lang::~Lang()
{
Platform::Console::WriteLine("~Lang(" + n0.ToString() + ")");
}
void Lang::Poo()
{
Platform::Console::WriteLine("Lang::Poo(" + n0.ToString() + ")");
if (sub != nullptr)
sub->Poo();
}<br/>
And then I tested it by adding this in InitializeComponent() of my main window:
{
auto a = ref new Lang(3);
a->Poo();
}
{
Lang a(2);
a.Poo();
}
And used break points to follow control flow (as neither std::cout nor Platform::Console::WriteLine seemed to work)
I'm a little confused now.
I was glad to see that the destructors were automatically called.
However a.Poo() (in the second block) caused a violation exception!!!
Apparently a.sub->sub->sub->n0 is the source of the memory access problem!
What is wrong here??