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

Constants immediatelly disappear from the Weak array. #8366

Closed
vicuna opened this issue Nov 12, 2003 · 2 comments
Closed

Constants immediatelly disappear from the Weak array. #8366

vicuna opened this issue Nov 12, 2003 · 2 comments

Comments

@vicuna
Copy link

vicuna commented Nov 12, 2003

Original bug ID: 1925
Reporter: administrator
Status: closed
Resolution: fixed
Priority: normal
Severity: feature
Category: ~DO NOT USE (was: OCaml general)

Bug description

Full_Name: Aleksey Nogin
Version: 3.07+2
OS: Red Hat Linux
Submission from: charter-182-203.caltech.edu (131.215.182.203)

Compare the following on 3.07+2 and 3.06:


    Objective Caml version 3.07+2

type 'a anchored_entry = { anchor : 'a; index: int };;

type 'a anchored_entry = { anchor : 'a; index : int; }

let memo = Weak.create 34;;

val memo : '_a Weak.t =

let make =

 let count = ref (-1) in
    fun data ->
       incr count;
       Weak.set memo !count (Some data);
       { anchor = data; index = !count };;

val make : '_a -> '_a anchored_entry =

let test ae =

 match Weak.get memo ae.index with
    Some data -> if ae.anchor != data then invalid_arg "Got a copy" else

data
| None -> invalid_arg "Entry went away";;
val test : '_a anchored_entry -> '_a =

test (make []);;

Exception: Invalid_argument "Entry went away".

test (make [1]);;

  • : int list = [1]

    Objective Caml version 3.06

type 'a ancored_entry = { ancor : 'a; index: int };;

type 'a ancored_entry = { ancor : 'a; index : int; }

let memo = Weak.create 34;;

val memo : '_a Weak.t =

mojave2:~> ocaml
Objective Caml version 3.06

type 'a anchored_entry = { anchor : 'a; index: int };;

type 'a anchored_entry = { anchor : 'a; index : int; }

let memo = Weak.create 34;;

val memo : '_a Weak.t =

let make =

 let count = ref (-1) in
    fun data ->
       incr count;
       Weak.set memo !count (Some data);
       { anchor = data; index = !count };;
      val make : '_a -> '_a anchored_entry = <fun>

let test ae =

 match Weak.get memo ae.index with
    Some data -> if ae.anchor != data then invalid_arg "Got a copy" else

data
| None -> invalid_arg "Entry went away";;
val test : '_a anchored_entry -> '_a =

test (make []);;

  • : '_a list = []

test (make [1]);;

  • : int list = [1]

Basically, in 3.07+2 [] disappears from the weak array as soon as it is added
into it. Is this a bug or a feature? The old behavior is useful because it
allows to assume that while a value is referenced somewhere, Weak.get will
always return Some. This allowed using Weak.get as an indicator of whether it
was OK to discard some "helper" data (which should be only discarded after the
primary data is no longer in use).

@vicuna
Copy link
Author

vicuna commented Nov 12, 2003

Comment author: administrator

On Wednesday, November 12, 2003, at 10:13 AM, Aleksey Nogin wrote:

When trying to switch from 3.06 to 3.07+2 I've noticed the following
difference in how the Weak module works:

[
In 3.06, an empty list [] does not disappear from a weak array;
in 3.07+2 it disappears immediately
]

Basically, in 3.07+2, the empty list disappears from the weak array as
soon as it is added into it. Is this a bug or a feature?

Feature.

The old behavior is useful because it allows to assume that while a
value is referenced somewhere, Weak.get will always return Some. This
allowed using Weak.get as an indicator of whether it was OK to discard
some "helper" data (which should be only discarded after the primary
data is no longer in use).

Yes, but. In 3.06, [] will never be removed from the weak array, and
you will keep your helper data forever.

The source your the problem is that [] is not allocated in the heap.
It is represented by an integer value. Hence, there is no sharing
between various occurrences of [] in your data and the notion of a
weak pointer to [] does not make sense.

If you really want to make sure that you are manipulating pointers,
you should use a record or a list ref.

-- Damien

@vicuna
Copy link
Author

vicuna commented Dec 29, 2003

Comment author: administrator

see also #8368 and #8370 -DD

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

1 participant