Skip to content

std.typecons: make RefCounted @safe#4692

Closed
wilzbach wants to merge 1 commit intodlang:masterfrom
wilzbach:make_ref_counted_safe
Closed

std.typecons: make RefCounted @safe#4692
wilzbach wants to merge 1 commit intodlang:masterfrom
wilzbach:make_ref_counted_safe

Conversation

@wilzbach
Copy link
Contributor

follow-up to #4685

}
import core.stdc.stdlib : free;
free(_refCounted._store);
() @trusted { free(_refCounted._store); }();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about this one. Is it safe to free memory from a pointer that we created and keep the reference too?

@schveiguy
Copy link
Member

What we also need is a check to make sure RefCounted becomes @system when a @system ctor or dtor is on the target type.

Thanks, this looks pretty good!

@schveiguy
Copy link
Member

Since we never expose a direct pointer to the ref counted payload, I think RefCounted can be perfectly safe.

Note, there is an existing issue for this: https://issues.dlang.org/show_bug.cgi?id=13983

@dnadlinger
Copy link
Contributor

dnadlinger commented Jul 29, 2016

Unless I'm missing something, we cannot currently make RefCounted @safe in that way since refCountedPayload, i.e. the AliasThis, returns a reference (which is the entire point of RefCounted). You'd need to make that @system to be able to mark the destructor @safe, which of course isn't any better.

@dnadlinger dnadlinger closed this Jul 29, 2016
@jmdavis
Copy link
Member

jmdavis commented Jul 29, 2016

Unless I'm missing something, we cannot currently make RefCounted @safe in that way since refCountedPayload, i.e. the AliasThis, returns a reference (which is the entire point of RefCounted). You'd need to make that @System to be able to mark the destructor @safe, which of course isn't any better.

Yes, and that's a good part of why Walter is currently working on adding a ref-counting solution to the language - because while you can make ref-counting solution with a library, you can't make an @safe ref-counting solution with a library.

@schveiguy
Copy link
Member

@klickverbot as long as you are in safe code, using the reference has to be while the ref-counted struct exists. You can't take the address of a ref in @safe code, so how is that unsafe?

@schveiguy
Copy link
Member

Nevermind, I just realized that you can return a ref, so that would be bad. Yeah, we have to wait on this.

@wilzbach wilzbach deleted the make_ref_counted_safe branch July 31, 2016 05:23
@nordlow
Copy link
Contributor

nordlow commented Oct 2, 2016

Why is RefCounted not pure?

Because of malloc, free, etc? If so I believe it should:

https://github.com/dlang/phobos/pull/4832/files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

Comments