-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Allow cleanup of an object through a destructor or dispose method #3691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Removed Type-Defect label. |
This comment was originally written by [email protected] Another use case of this is when wrapping JavaScript objects as Dart objects that have to retain a reference to the wrapped object. For instance: class EditSession { EditSession.proxy(js.Proxy session) { // ... void dispose() { Right now you have to basically manually do garbage collection on Javascript objects that need to have a longer life time. |
Preferably, I'd like to not make garbage collection visible. Right now, the spec does not say anything about garbage collection. If an object is no longer reachable, the spec doesn't say what should happen to it. It is a valid implementation to keep it alive forever (it's just not very memory efficient). If we start making it visible when an object is no longer reachable, by calling a dispose method, then we either get unpredictable behavior, or we have to specify when this callback should happen, which will only put restrictions on what the VM can do (and will likely be hard or impossible in dart2js code for a long time, depending on which GC-related features goes into ES6). |
Another use case is when you create a class to represent a dom object. The class old a reference to the Element in the page and when is disposed the dom element should be removed. |
This comment was originally written by [email protected] Please implement it to make possible RAII, it's important for server-side. |
This comment was originally written by @Emasoft Garbage collection should not be visible. But you must be able to manually destroy any object exacly like the Garbage collector would do. In this way the Garbage collector will have one less object to worry about. if(sessionProxy.referenceCount == 0) |
We could make transparent week ptr syntax. I mean, standard technique for implementing manual destruction in gc languages, its to create shared object, which containing single reference to payload. It could be compromise, afa it had lenient syntax. ( So, it acts like regular object reference, (you should compare with null), but also changes shared to all references, which pretty much similar to real destruct. In other words - GC threats objects with no references as removed, by reversing this idea (removed object would have no references), we bringing almost RAII. |
What happened to this? |
There are no current plans to add something like this to the Dart language. The primary reasons is that it won't be easy (or perhaps even possible_ to compile it to JavaScript. Even the JavaScript We could still do that, but it adds complexity to the memory model. So, for now, this feature is not seen as important enough to be on our list of priorities. |
Got it.
Thanks for clarifying !
…On Fri, 25 Jan 2019 at 14:49, Lasse R.H. Nielsen ***@***.***> wrote:
There are no current plans to add something like this to the Dart language.
The primary reasons is that it won't be easy (or perhaps even possible_ to
compile it to JavaScript. Even the JavaScript WeakMap doesn't provide a
callback when an object is no longer reachable. So, the feature would be VM
only.
We could still do that, but it adds complexity to the memory model.
So, for now, this feature is not seen as important enough to be on our
list of priorities.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#3691 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AMJb5V92ygeo3LGw63m4Sm21DB_rtPEZks5vGwtggaJpZM4E3-Il>
.
|
With FFI being available #34452, I think destructor should get back to the spotlight. |
@lrhn JS is on the way to get @truongsinh |
Issue tracking |
I did not quite understand if Dart already supports finalizers/destructors ? |
You can add finalizers in C code with the At some point in the future, you can add finalizers in Dart executing Dart code as well, but that hasn't been implemented yet: |
This issue was originally filed by [email protected]
Dart should have a destructor or dispose method <http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx>, for when an object is marked to be collected.
To give a use case where this would be useful lets say you had a Texture class which contains a WebGLTexture. When you're done using
the WebGLTexture it should be disposed of by calling deleteTexture
through a WebGLRenderingContext. So if you were sharing this Texture
between other objects it isn't entirely clear when its okay to call
deleteTexture. A destructor or dispose method would provide a clear place to call deleteTexture safely.
Having a destructor or dispose method could also be of use for those trying to embed Dart in a C/C++ program.
The text was updated successfully, but these errors were encountered: