www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - this is null

reply ANtlord <antlord92 gmail.com> writes:
Hello everyone! I've encountered the problem which I already 
encountered before. Unfortunately, I had no time in the previous 
time to report and to talk about it. So I decided to play making 
my own "malloc" function in pure D (betterC) at this time. And I 
encountered the issue one more time. `this` can be null. How? 
Please take a look at my project [0]. It gets the current heap 
break and tries to increase via a free list. So the segfault I 
meet happens there [1]. Tell me, please. What do I wrong?

[0] https://github.com/ANtlord/deadmemory
[1] 
https://github.com/ANtlord/deadmemory/blob/master/src/deadmemory/freelist.d#L56
Mar 09 2019
parent reply Paul Backus <snarwin gmail.com> writes:
On Saturday, 9 March 2019 at 19:18:38 UTC, ANtlord wrote:
 Hello everyone! I've encountered the problem which I already 
 encountered before. Unfortunately, I had no time in the 
 previous time to report and to talk about it. So I decided to 
 play making my own "malloc" function in pure D (betterC) at 
 this time. And I encountered the issue one more time. `this` 
 can be null. How? Please take a look at my project [0]. It gets 
 the current heap break and tries to increase via a free list. 
 So the segfault I meet happens there [1]. Tell me, please. What 
 do I wrong?

 [0] https://github.com/ANtlord/deadmemory
 [1] 
 https://github.com/ANtlord/deadmemory/blob/master/src/deadmemory/freelist.d#L56
You can end up with a null `this` reference if you dereference a null pointer to a struct and then call a method on the result. For example: struct S { bool isThisNull() { return &this is null; } } void main() { import.std.stdio; S* p = null; writeln((*p).isThisNull); // true } Interactive version: https://run.dlang.io/is/fgT2rS
Mar 09 2019
next sibling parent reply ANtlord <antlord92 gmail.com> writes:
On Saturday, 9 March 2019 at 20:04:53 UTC, Paul Backus wrote:
 You can end up with a null `this` reference if you dereference 
 a null pointer to a struct and then call a method on the 
 result. For example:
I can but my reference is not null before calling. Take a look at the line of code [0]. There is a check before the line. https://github.com/ANtlord/deadmemory/blob/master/src/deadmemory/mem.d#L20 [0]
Mar 09 2019
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 03/09/2019 12:10 PM, ANtlord wrote:
 On Saturday, 9 March 2019 at 20:04:53 UTC, Paul Backus wrote:
 You can end up with a null `this` reference if you dereference a null 
 pointer to a struct and then call a method on the result. For example:
I can but my reference is not null before calling. Take a look at the line of code [0]. There is a check before the line. https://github.com/ANtlord/deadmemory/blob/master/src/deadmemory/mem.d#L20 [0]
I haven't run the code but which pointer is null? Try adding this check as well: auto node = this.list.getFisrtFreeOrAdd(memViewLen); assert(node !is null); Ali
Mar 09 2019
parent ANtlord <antlord92 gmail.com> writes:
On Saturday, 9 March 2019 at 21:00:51 UTC, Ali Çehreli wrote:
 I haven't run the code but which pointer is null? Try adding
I mean `this` by "this" word. You can see that `this` is null if you run gdb and before that line make `p/x this` [0]
 this check as well:

   auto node = this.list.getFisrtFreeOrAdd(memViewLen);
   assert(node !is null);
I get segfault in `getFisrtFreeOrAdd` method. Before the line I have an assertion [1]. It looks like the program misses (I have no idea how) `list` object while calling its method `getFisrtFreeOrAdd`. [0] https://github.com/ANtlord/deadmemory/blob/master/src/deadmemory/freelist.d#L56 [1] https://github.com/ANtlord/deadmemory/blob/master/src/deadmemory/mem.d#L19
Mar 09 2019
prev sibling parent reply spir <denis.spir gmail.com> writes:
On 09/03/2019 21:10, ANtlord via Digitalmars-d-learn wrote:
 On Saturday, 9 March 2019 at 20:04:53 UTC, Paul Backus wrote:
 You can end up with a null `this` reference if you dereference a null pointer 
 to a struct and then call a method on the result. For example:
I can but my reference is not null before calling. Take a look at the line of code [0]. There is a check before the line. https://github.com/ANtlord/deadmemory/blob/master/src/deadmemory/mem.d#L20 [0]
There is a typo in this instruction: T* ptr = this.list.getFisrtFreeOrAdd(memViewLen).getPtr!T(); ^^ rs (may this explain your null? the compiler should complain) diniz
Mar 10 2019
parent ANtlord <antlord92 gmail.com> writes:
On Sunday, 10 March 2019 at 14:25:56 UTC, spir wrote:

 There is a typo in this instruction:

 T* ptr = this.list.getFisrtFreeOrAdd(memViewLen).getPtr!T();
                         ^^
                         rs
 (may this explain your null? the compiler should complain)

 diniz
Good catch! But I have the same typo within the definition of the method. I believe DMD screams about undefined method if it happens.
Mar 10 2019
prev sibling parent ANtlord <antlord92 gmail.com> writes:
On Saturday, 9 March 2019 at 20:04:53 UTC, Paul Backus wrote:
 struct S
 {
     bool isThisNull() { return &this is null; }
 }

 void main()
 {
     import.std.stdio;
     S* p = null;
     writeln((*p).isThisNull); // true
 }

 Interactive version: https://run.dlang.io/is/fgT2rS
Anyway, thank you! I didn't know about the feature.
Mar 09 2019