Skip to content
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

Finaliser that are really called when a value will be freed. #7210

Closed
vicuna opened this issue Apr 4, 2016 · 4 comments
Closed

Finaliser that are really called when a value will be freed. #7210

vicuna opened this issue Apr 4, 2016 · 4 comments

Comments

@vicuna
Copy link

vicuna commented Apr 4, 2016

Original bug ID: 7210
Reporter: bobot
Assigned to: @mshinwell
Status: resolved (set by @mshinwell on 2016-12-08T07:41:12Z)
Resolution: fixed
Priority: normal
Severity: feature
Fixed in version: 4.04.0
Category: runtime system and C interface
Monitored by: @diml

Bug description

Since the values finalized are given as argument of their finalizers, they are kept alive. So you can't be sure that nobody will ever use this value again.

Before 4.03 the finalizer where called after cleaning weak pointers, thus possibly breaking invariant of weakset, hashconsing. In 4.03 they are called before cleaning weak pointers (which is done when we are sure to sweep the value).

It could be interesting to give the possibility to register finalizers that would not take the value as argument but instead unit and would be called when we are sure to sweep the value.

The question is not really how to add this feature (straightforward), more if ocaml needs it.

@vicuna
Copy link
Author

vicuna commented Apr 4, 2016

Comment author: @alainfrisch

Just to be sure to understand the (potential) problem: a finalizer could create a reference to the value being finalized (e.g. by storing it in a global table). If a single finalizer is attached to a value, it would be responsible for this situation, and if it does create a reference to the value, well, it knows that the value won't be collected. I don't think we need to protect against that. A more tricky case is when multiple finalizers are attached to the same value (or to nested values), in which case it's much harder to attach the "responsibility" to any piece of code. Is that the situation you have in mind?

@vicuna
Copy link
Author

vicuna commented Apr 18, 2016

Comment author: @lpw25

This would be very useful in combination with weak pointers.

As of 4.03, it is difficult to "clean up" a data structure containing a weak pointer when the weak pointer is cleared (e.g. removing an entry from a weak hash table once its weak pointer is empty). Previously this could be done with ordinary finalisers, but that is no longer possible since finalisers are now run before weak pointers are cleared.

Ordinary finalisers are now run before clearing weak pointers to make sure that weak pointers are only cleared once the object is genuinely dead -- a useful invariant for things like hash-consing. Since the proposed alternative finalisers do not receive their argument and cannot bring it back to life, they can safely be run after weak pointers have been cleared without breaking the desired invariant on weak pointers.

@vicuna
Copy link
Author

vicuna commented Apr 27, 2016

Comment author: bobot

GPR at #562

@vicuna
Copy link
Author

vicuna commented Dec 8, 2016

Comment author: @mshinwell

Fixed in 4.04

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

No branches or pull requests

2 participants