digitalmars.D.learn - &this access violation
- Trevor Parscal (76/76) Jun 19 2005 When I do this... (compilable code)
- Vathix (9/80) Jun 19 2005 You probably shouldn't even assume 'this' is an lvalue, but if it happen...
- Trevor Parscal (7/14) Jun 19 2005 Yes, good old C++ habbits die hard. Thanks for the help.
When I do this... (compilable code)
// start of test.d //
import std.stdio;
import std.string;
class ROOT
{
/* Members */
public:
BRANCH*[] Branches;
char[] Name;
/* Functions */
public:
void Add(inout BRANCH _branch)
{
_branch.Root = &this;
this.Branches ~= &_branch;
}
void Check()
{
writefln("ROOT: " ~ this.Name);
foreach(BRANCH* branch; this.Branches)
{
branch.Check();
}
}
};
class BRANCH
{
/* Members */
public:
ROOT* Root;
BRANCH*[] Branches;
/* Functions */
public:
void Add(inout BRANCH _branch)
{
_branch.Root = this.Root;
this.Branches ~= &_branch;
}
void Check()
{
writefln("BRANCH: " ~ this.Root.Name);
// Access Violation happens here in 0.025
foreach(BRANCH* branch; this.Branches)
{
branch.Check();
}
}
};
int main()
{
ROOT root = new ROOT();
root.Name = "test";
BRANCH branch1 = new BRANCH();
BRANCH branch2 = new BRANCH();
root.Add(branch1);
branch1.Add(branch2);
root.Check();
return 0;
}
// end of test.d //
It outputs this...
//////////////////
ROOT: test
BRANCH:
BRANCH:
//////////////////
In 0.025, it would give me an access violation..
I think it has to do with the "&this" not mapping to the right place.
Maybe I am wrong.
Any ideas?
--
Thanks,
Trevor Parscal
www.trevorparscal.com
trevorparscal hotmail.com
Jun 19 2005
On Sun, 19 Jun 2005 06:54:50 -0400, Trevor Parscal
<trevorparscal hotmail.com> wrote:
When I do this... (compilable code)
// start of test.d //
import std.stdio;
import std.string;
class ROOT
{
/* Members */
public:
BRANCH*[] Branches;
char[] Name;
/* Functions */
public:
void Add(inout BRANCH _branch)
{
_branch.Root = &this;
this.Branches ~= &_branch;
}
void Check()
{
writefln("ROOT: " ~ this.Name);
foreach(BRANCH* branch; this.Branches)
{
branch.Check();
}
}
};
class BRANCH
{
/* Members */
public:
ROOT* Root;
BRANCH*[] Branches;
/* Functions */
public:
void Add(inout BRANCH _branch)
{
_branch.Root = this.Root;
this.Branches ~= &_branch;
}
void Check()
{
writefln("BRANCH: " ~ this.Root.Name);
// Access Violation happens here in 0.025
foreach(BRANCH* branch; this.Branches)
{
branch.Check();
}
}
};
int main()
{
ROOT root = new ROOT();
root.Name = "test";
BRANCH branch1 = new BRANCH();
BRANCH branch2 = new BRANCH();
root.Add(branch1);
branch1.Add(branch2);
root.Check();
return 0;
}
// end of test.d //
It outputs this...
//////////////////
ROOT: test
BRANCH:
BRANCH:
//////////////////
In 0.025, it would give me an access violation..
I think it has to do with the "&this" not mapping to the right place.
Maybe I am wrong.
Any ideas?
You probably shouldn't even assume 'this' is an lvalue, but if it happens
to be one, you don't even know where it's stored. I'm assuming it's just a
local variable which doesn't exist after Add() returns. You seem to be
assuming classes are handled like c++ classes. In D, classes only use
references, which are pointers behind the scenes. So ROOT* is actually a
pointer to a pointer. You should only need ROOT root; and set it to 'this'
in Add() and all will be swell.
Jun 19 2005
Vathix wrote:You probably shouldn't even assume 'this' is an lvalue, but if it happens to be one, you don't even know where it's stored. I'm assuming it's just a local variable which doesn't exist after Add() returns. You seem to be assuming classes are handled like c++ classes. In D, classes only use references, which are pointers behind the scenes. So ROOT* is actually a pointer to a pointer. You should only need ROOT root; and set it to 'this' in Add() and all will be swell.Yes, good old C++ habbits die hard. Thanks for the help. -- Thanks, Trevor Parscal www.trevorparscal.com trevorparscal hotmail.com
Jun 19 2005








Trevor Parscal <trevorparscal hotmail.com>