HerpeDerpeDerp
Getting started with source analyzers
Regardless of whether or not this is a good way of solving the problem of references to removed objects lingering around, I would also just like to learn a bit about things like analyzers and source generators in general as I've never used them yet. That's why I asked specifically about analyzers. Even if they're not a good solution to the problem, I would still like to know if/how they could be used
21 replies
Getting started with source analyzers
I'm making a game and in that game there are several types of objects that exist for some time but can be removed at some point. Many of these objects will have references to each other, such as a projectile keeping track of who fired it because it needs that information when it hits something. It is in theory possible to not have such references between objects, but I don't really see any method that doesn't have major drawbacks.
Problems arise when such objects get removed as references to these removed objects might still exist somewhere and attempting to do things with these removed objects through those references can lead to all kinds of bugs. Simply flagging the instance as being removed and always checking the flag before doing something with the instance seems like an easy solution, although still very easy to miss some of the (tens of) thousands of checks, but many of those instances will be reused to avoid memory allocations and a simple flag won't do. The solutions I'm currently leaning towards is giving each instance a version number which is incremented each time the instance is removed. Whenever one objects stores a reference to another objects, it does this wrapped in a struct that stores the current version number (as of making the struct) of the referenced instance with it. If I try to get the reference from the struct, it first compares the current version against the stored version number and if they are not equal it won't give me the reference but instead just null. It is still perfectly fine for functions to pass references to each other directly without the wrappers because the removal of objects only happens at very specific moments so it suffices that those unwrapped references are just never stored.
21 replies