Design question w/ weak references

I'm designing an AudioMixer thing, and the way I currently have it designed is that calling returns an AudioInstance. When that AudioInstance is dropped, the clip stops playing.

So far so good.

Now - what I'd like to do, is add a .pause_all() method to AudioMixer which will loop through all the instances and pause them.

This means the mixer now needs to also keep a reference to the instances - but I don't want it to count towards ownership. I.e. if AudioInstance is dropped outside of the mixer, it should be properly dropped (and stop playing).

So I'm thinking that instead of play() returning AudioInstance, it will return Rc<AudioInstance> and internally add a weak reference to an internal collection. Then pause_all() will loop through all of these, and for all that are upgradeable, pause the clip.

I think, up until here, also fine... but here comes the question-

Do I need to clean up that collection? (i.e. do weak references cost much to just keep pushing to an ever-growing Vec? like... I don't think the number would ever get past 10,000 or so)

If I do, I suppose it's not so bad... each instance could be given a unique id and a reference to the collection, and clean itself up when dropped (a structure like beach_map could work here).. this might also be better overall since it gives me more power in the mixer API to do things by id, but just wondering if this is really necessary...

If you have a weak reference, that memory won't be deallocated. Note the AudioInstance will be dropped, but the backing memory will remain until the last weak is dropped.

1 Like

oh.. I did not realize that. Thanks! Ok so adding the cleanup is required :sweat_smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.