www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - call to super trashes pointer?

reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
struct A
{
int blah;
}
class B
{
     A* a;
     this(A* _a)
     {
         writeln(_a)
         a =_a;
     }
}

class C : B
{
     this(A* _a)
     {
         writeln(_a)
         super(_a);
     }
}
int main(string[] args)
{
         A a;
         writeln(&a);
         C c = new C(&a);
}

prints
7FFF56E787F8
7FFF56E787F8
null

??? What is happening here?
Feb 23 2016
next sibling parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Tuesday, 23 February 2016 at 09:16:08 UTC, Nicholas Wilson 
wrote:
 struct A
 {
 int blah;
 }
 class B
 {
     A* a;
     this(A* _a)
     {
         writeln(_a)
         a =_a;
     }
 }

 class C : B
 {
     this(A* _a)
     {
         writeln(_a)
         super(_a);
     }
 }
 int main(string[] args)
 {
         A a;
         writeln(&a);
         C c = new C(&a);
 }

 prints
 7FFF56E787F8
 7FFF56E787F8
 null

 ??? What is happening here?
fwiw I can reassign the reference (in the classes) to a after B's call to super but still ...
Feb 23 2016
prev sibling parent reply Rene Zwanenburg <renezwanenburg gmail.com> writes:
On Tuesday, 23 February 2016 at 09:16:08 UTC, Nicholas Wilson 
wrote:
 struct A
 {
 int blah;
 }
 class B
 {
     A* a;
     this(A* _a)
     {
         writeln(_a)
         a =_a;
     }
 }

 class C : B
 {
     this(A* _a)
     {
         writeln(_a)
         super(_a);
     }
 }
 int main(string[] args)
 {
         A a;
         writeln(&a);
         C c = new C(&a);
 }

 prints
 7FFF56E787F8
 7FFF56E787F8
 null

 ??? What is happening here?
I can't reproduce this. After making a few changes to get the code to compile (adding ; after the first two writeln's, and changing int main to void main), everything works as expected. Which compiler and version are you using?
Feb 23 2016
parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Tuesday, 23 February 2016 at 10:47:17 UTC, Rene Zwanenburg 
wrote:
 On Tuesday, 23 February 2016 at 09:16:08 UTC, Nicholas Wilson 
 wrote:
 struct A
 {
 int blah;
 }
 class B
 {
     A* a;
     this(A* _a)
     {
         writeln(_a)
         a =_a;
     }
 }

 class C : B
 {
     this(A* _a)
     {
         writeln(_a)
         super(_a);
     }
 }
 int main(string[] args)
 {
         A a;
         writeln(&a);
         C c = new C(&a);
 }

 prints
 7FFF56E787F8
 7FFF56E787F8
 null

 ??? What is happening here?
I can't reproduce this. After making a few changes to get the code to compile (adding ; after the first two writeln's, and changing int main to void main), everything works as expected. Which compiler and version are you using?
v2.067-devel-e431363 I "fixed" it see above.
Feb 23 2016
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 2/23/16 6:05 AM, Nicholas Wilson wrote:
 On Tuesday, 23 February 2016 at 10:47:17 UTC, Rene Zwanenburg wrote:
 On Tuesday, 23 February 2016 at 09:16:08 UTC, Nicholas Wilson wrote:
 struct A
 {
 int blah;
 }
 class B
 {
     A* a;
     this(A* _a)
     {
         writeln(_a)
         a =_a;
     }
 }

 class C : B
 {
     this(A* _a)
     {
         writeln(_a)
         super(_a);
     }
 }
 int main(string[] args)
 {
         A a;
         writeln(&a);
         C c = new C(&a);
 }

 prints
 7FFF56E787F8
 7FFF56E787F8
 null

 ??? What is happening here?
I can't reproduce this. After making a few changes to get the code to compile (adding ; after the first two writeln's, and changing int main to void main), everything works as expected. Which compiler and version are you using?
v2.067-devel-e431363 I "fixed" it see above.
This is not a released version. Using both 2.067.1 and 2.068.0, I get the expected behavior. There could have been a regression fixed between that version and the release, I suggest you try a released version: http://dlang.org/download.html#dmd -Steve
Feb 23 2016