Thread overview
this is null
Mar 09, 2019
ANtlord
Mar 09, 2019
Paul Backus
Mar 09, 2019
ANtlord
Mar 09, 2019
Ali Çehreli
Mar 09, 2019
ANtlord
Mar 10, 2019
spir
Mar 10, 2019
ANtlord
Mar 09, 2019
ANtlord
March 09, 2019
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
March 09, 2019
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
March 09, 2019
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]
March 09, 2019
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.
March 09, 2019
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
March 09, 2019
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


March 10, 2019
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


March 10, 2019
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.