SoundSource (FilePlayer, SamplePlayer): freePlayer vs dealloc?

SamplePlayer API has method sound -> sampleplayer -> freePlayer,
and same for FilePlayer, and both of them is SoundSource (it's kinda inheritance).

But accordig the doc for sound -> channel -> addCallbackSource that returns new SoundSource,

The caller takes ownership of the allocated SoundSource, and should free it with

playdate->system->realloc(source, 0);

So, my question is,
when and in what cases should I call realloc and when freePlayer?

2 Likes

If you create a FilePlayer with snd->fileplayer->newPlayer(), you should free it with freePlayer(). If you create a callback SoundSource with addCallbackSource(), you shouldn't free it with freePlayer() because that's for FilePlayers.

That said.. I added retain counting to the sound objects recently and now all of the freeX() functions just call

static void s_releaseSource(void* source)
{
	if ( source )
	{
		SoundLib_removeSource(source);
		SoundSource_release(source);
	}
}

We should be using that for the callback source as well--if something else is holding a retain on it we shouldn't just free the object. I'll file this, and in the mean time you're probably okay just freeing it with realloc(). Or, hell, go ahead and use fileplayer->freePlayer() because it's actually doing the right thing. Just note that it might break in the future.

1 Like

So, just to ensure, currently I have to remove source before to free it.
But saying "retain counting" do you mean that in some future release you'll implement some ref-counting with ptr-relations? That probably could be good :man_shrugging:t2: but what cost of it will be?