Re: this is null

2019-03-10 Thread ANtlord via Digitalmars-d-learn

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.


Re: this is null

2019-03-10 Thread spir via Digitalmars-d-learn

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




Re: this is null

2019-03-09 Thread ANtlord via Digitalmars-d-learn

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





Re: this is null

2019-03-09 Thread Ali Çehreli via Digitalmars-d-learn

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


Re: this is null

2019-03-09 Thread ANtlord via Digitalmars-d-learn

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.


Re: this is null

2019-03-09 Thread ANtlord via Digitalmars-d-learn

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]


Re: this is null

2019-03-09 Thread Paul Backus via Digitalmars-d-learn

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


this is null

2019-03-09 Thread ANtlord via Digitalmars-d-learn
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


Re: opCast fails when this is null.

2017-10-28 Thread Mike Wey via Digitalmars-d-learn

On 28-10-17 16:22, Nicholas Wilson wrote:

On Saturday, 28 October 2017 at 14:19:01 UTC, Nicholas Wilson wrote:
As Basile mentioned, this is compiler sticking checks in behind your 
back.
The reason it works on new LDC is because #6982 was cherry picked to 
LDC (1.3?) before it was merged into dmd (not sure what version, I 
though it was 2.076, but it might have been one of the betas of 2.077) 
because I needed it for DCompute to build without -release.


The only course is to use recent compilers.


Erm, 2.077 is not a thing yet. Does it work with 2.076.1?

No, it does work with the 2.077 beta tough.

--
Mike Wey


Re: opCast fails when this is null.

2017-10-28 Thread Nicholas Wilson via Digitalmars-d-learn
On Saturday, 28 October 2017 at 14:19:01 UTC, Nicholas Wilson 
wrote:
As Basile mentioned, this is compiler sticking checks in behind 
your back.
The reason it works on new LDC is because #6982 was cherry 
picked to LDC (1.3?) before it was merged into dmd (not sure 
what version, I though it was 2.076, but it might have been one 
of the betas of 2.077) because I needed it for DCompute to 
build without -release.


The only course is to use recent compilers.


Erm, 2.077 is not a thing yet. Does it work with 2.076.1?


Re: opCast fails when this is null.

2017-10-28 Thread Nicholas Wilson via Digitalmars-d-learn

On Saturday, 28 October 2017 at 13:24:49 UTC, Mike Wey wrote:
The following code runs correctly when compiled with ldc 
(1.4.0) but fails with an assert error when compiled with dmd 
(2.076 and ldc 1.2.0)



```
class A
{

}

class B
{
T opCast(T)()
{
return this;
}
}

void main()
{
A a = null;
B b = null;

auto c = cast(Object)a;
	auto d = cast(Object)b; // Fails with: 
core.exception.AssertError@test.d(8): null this

}
```

How would you write an opCast that would handle this case 
correctly?


Testing if this is null at the start of the opCast doesn't help 
since the assert is thrown before that happens.
Making the opCast static leaves us without access to this, 
which would be needed in my use case.
We can't relay on ufcs since the rewrite to opCast doesn't 
happen when it's not a member function.


As Basile mentioned, this is compiler sticking checks in behind 
your back.
The reason it works on new LDC is because #6982 was cherry picked 
to LDC (1.3?) before it was merged into dmd (not sure what 
version, I though it was 2.076, but it might have been one of the 
betas of 2.077) because I needed it for DCompute to build without 
-release.


The only course is to use recent compilers.


Re: opCast fails when this is null.

2017-10-28 Thread Basile B. via Digitalmars-d-learn

On Saturday, 28 October 2017 at 13:24:49 UTC, Mike Wey wrote:
The following code runs correctly when compiled with ldc 
(1.4.0) but fails with an assert error when compiled with dmd 
(2.076 and ldc 1.2.0)



```
class A
{

}

class B
{
T opCast(T)()
{
return this;
}
}

void main()
{
A a = null;
B b = null;

auto c = cast(Object)a;
	auto d = cast(Object)b; // Fails with: 
core.exception.AssertError@test.d(8): null this

}
```

How would you write an opCast that would handle this case 
correctly?


Compiler change is required.

This doesn't happen in -release mode. The assert is automatically 
generated by the compiler. It could be disabled, as done here for 
ctors and dtors


https://github.com/dlang/dmd/pull/6982


opCast fails when this is null.

2017-10-28 Thread Mike Wey via Digitalmars-d-learn
The following code runs correctly when compiled with ldc (1.4.0) but 
fails with an assert error when compiled with dmd (2.076 and ldc 1.2.0)



```
class A
{

}

class B
{
T opCast(T)()
{
return this;
}
}

void main()
{
A a = null;
B b = null;

auto c = cast(Object)a;
	auto d = cast(Object)b; // Fails with: 
core.exception.AssertError@test.d(8): null this

}
```

How would you write an opCast that would handle this case correctly?

Testing if this is null at the start of the opCast doesn't help since 
the assert is thrown before that happens.
Making the opCast static leaves us without access to this, which would 
be needed in my use case.
We can't relay on ufcs since the rewrite to opCast doesn't happen when 
it's not a member function.


--
Mike Wey