I have an implemented class that provides "make_new" to create a related object. That related object has a UUID, and the object is a "singleton" per UUID; that is for UUID "5", there is only one object.
To implement this, I was going to keep a table of existing created objects, and if the constructor is called for an existing UUID, then it would simply return the existing object. I was going to use a µPy object to hold these, internally (maybe a set?). In any case, if there aren't any _external_ references (in user python code), then I'd like to discard the objects.
Questions:
If I have an "internal" mp_obj_t (say, made with mp_obj_new_list), will that be safe from the GC?
If I store objects inside this "internal" structure, how can I tell if they're referenced "outside"?
If they're not referenced outside (thus not needed internally, either), how can I mark them for reaping by the GC?
"weak" reference to python object?
Re: "weak" reference to python object?
you probably want a dict rather than a set. The key would be the uuid and the value would be the singleton object for that uuid.MrSurly wrote:I have an implemented class that provides "make_new" to create a related object. That related object has a UUID, and the object is a "singleton" per UUID; that is for UUID "5", there is only one object.
To implement this, I was going to keep a table of existing created objects, and if the constructor is called for an existing UUID, then it would simply return the existing object. I was going to use a µPy object to hold these, internally (maybe a set?). In any case, if there aren't any _external_ references (in user python code), then I'd like to discard the objects.
As long as the internal object is referred to by some object on the heap, or otherwise through a root pointer.Questions:
If I have an "internal" mp_obj_t (say, made with mp_obj_new_list), will that be safe from the GC?
You can't (at least not directly). The GC uses a mark and sweep algorithim. It clears all of the "marks" for each object in the heap, then starts with the root pointers and marks every referenced object as "in-use". When its done, any objects which are not marked as "in-use" can be freed.If I store objects inside this "internal" structure, how can I tell if they're referenced "outside"?
You don't need to do anything. When nothing else references them, then the GC will reap them then next time collect is called.If they're not referenced outside (thus not needed internally, either), how can I mark them for reaping by the GC?
Re: "weak" reference to python object?
Does that mean that there's no weakref concept for µPy objects?
Re: "weak" reference to python object?
Not that I'm aware of. If there is a reference to an object then it doesn't get freed.
The issue with weakrefs (that I see) is that the GC would need to zero out the reference and the GC doesn't really know if a reference is a pointer or an integer that just happens to have an unfortunate value.
I suppose if the GC knew for sure that an object was a weakref object then it could work with it, but it feels like quite a bit of complexity.
The issue with weakrefs (that I see) is that the GC would need to zero out the reference and the GC doesn't really know if a reference is a pointer or an integer that just happens to have an unfortunate value.
I suppose if the GC knew for sure that an object was a weakref object then it could work with it, but it feels like quite a bit of complexity.
Re: "weak" reference to python object?
it seems finalizers support is implemented in VM. but no way provided to use them from Python.
https://github.com/micropython/micropython/issues/245
related:
__del__ special method not implemented for user-defined classes https://github.com/micropython/micropyt ... ssues/1878
i would be interested for discussion around the idea of an "uweak" object :
Do we want to have weakref support? https://github.com/micropython/micropyt ... -530262406
https://github.com/micropython/micropython/issues/245
related:
__del__ special method not implemented for user-defined classes https://github.com/micropython/micropyt ... ssues/1878
i would be interested for discussion around the idea of an "uweak" object :
Do we want to have weakref support? https://github.com/micropython/micropyt ... -530262406