Hi,
I wonder why, in data structures, deleting a scalar invalidates all pointers to any other scalar in the same canvas.
Is this a design choice ?
I'm asking this because I planned to use pointers inside abstractions to handle scalars in a patch, for example for making them move.
But may be I'm wrong, because if every time a scalar is deleted all [pointer]s have to [traverse( the patch to retrieve the scalar they pointed to, it seems not so optimized.
Thanks in advance
Regards
JYG
Hi,
yes, it's a design choice. A "gpointer" is basically a safe handle to a data structure object (scalar or array) and as such it has to know when it has been invalidated. There are several ways to implement this.
For example, every data structure object could maintain a list of all gpointers that currently reference it. When the object is deleted, it would have to notify all "gpointers". Conversely, when a gpointer is set to another object, it has to unregister from the old object and register to the new one. In C, gpointers and stubs are represented by the "t_gpointer" resp. "t_gstub" structs (see m_pd.h)
Miller chose a simpler solution: every canvas has a "stub" object with a reference count and a generation counter. When you set a gpointer, it not only saves a pointer to the data structure object, it also keeps a reference to the stub of the owning canvas. When a data structure object is deleted, the stub's generation counter is incremented and as a consequence all existing gpointers to *any* data structure object in that canvas are invalidated.
Unfortunately, since "t_gpointer" and "t_scalar" are public structs, I don't think there is a way to change the implementation without breaking backwards compatibility. I'm not even sure it would be worth the additional complexity.
Actually, I have had the same problem as you. The solution I've found is to never actually delete the scalars, instead I made the scalar invisible and set a special field inside scalar. When I wanted to create a scalar, I first traversed the canvas to see if there is already a "dead" scalar I could reuse. Of course, this only works as long as the user never *directly* deletes the scalars (e.g. with select + delete). Otherwise, the only solution is to re-traverse the canvas and update all your [pointer] objects...
Christof
On 19.10.2024 22:45, Jean-Yves Gratius wrote:
Hi,
I wonder why, in data structures, deleting a scalar invalidates all pointers to any other scalar in the same canvas.
Is this a design choice ?
I'm asking this because I planned to use pointers inside abstractions to handle scalars in a patch, for example for making them move.
But may be I'm wrong, because if every time a scalar is deleted all [pointer]s have to [traverse( the patch to retrieve the scalar they pointed to, it seems not so optimized.
Thanks in advance
Regards
JYG
pd-list@lists.iem.at - the Pure Data mailinglist https://lists.iem.at/hyperkitty/list/pd-list@lists.iem.at/message/KVZQ3VBBJN...
To unsubscribe send an email to pd-list-leave@lists.iem.at mailing list UNSUBSCRIBE and account-management -> https://lists.iem.at/
Do you think it would be acceptable if we could let the [pointer] object store the canvas to which the glist is referring, even when the referring scalar has been deleted ? This way, we should be able to send messages to the canvas (using send-window message), without the need to explicitly know what is the canvas name ...
On 20.10.2024 14:35, jyg@gumo.fr wrote:
Do you think it would be acceptable if we could let the [pointer] object store the canvas to which the glist is referring, even when the referring scalar has been deleted ?
First off, glist and canvas are the same thing.
In theory, [pointer] *could* obtain the head of the owning glist of an invalidated gpointer, as long as the stub hasn't been cut off (i.e. the glist hasn't been deleted). This could be a new method, e.g. "glist", maybe with an extra argument to specify whether invalidated pointers should be considered or not.
Personally, I find it hard to imagine a use case, though. If you want to update all [pointer] objects after a scalar has been deleted, this is likely done from a central place, so I don't see why you can't just store the canvas name.
This way, we should be able to send messages to the canvas (using send-window message), without the need to explicitly know what is the canvas name ...
pd-list@lists.iem.at - the Pure Data mailinglist https://lists.iem.at/hyperkitty/list/pd-list@lists.iem.at/message/GBSW4MIIFS...
To unsubscribe send an email to pd-list-leave@lists.iem.at mailing list UNSUBSCRIBE and account-management -> https://lists.iem.at/