freeing pwfx after waveOutOpen throws

95 Views Asked by At

According to Microsoft documentation: "You can free this [pwfx] structure immediately after passing it to waveOutOpen."

But this code dosen't seem to agree:

pwfx=new WAVEFORMATEX;
pwfx->wFormatTag=WAVE_FORMAT_PCM;
pwfx->nChannels=2;
pwfx->nSamplesPerSec=SPS;
pwfx->nAvgBytesPerSec=SPS*2;
pwfx->nBlockAlign=2;
pwfx->wBitsPerSample=8;
mmres=waveOutOpen(&ghwo,uDeviceID,pwfx,dwCallback,dwCallbackInstance,fdwOpen);
delete pwfx;

screen picture with code & call stack

2

There are 2 best solutions below

3
Paul Sanders On

You don't need to new or delete anything. You can just do:

WAVEFORMATEX wfx = { };
wfx.wFormatTag=WAVE_FORMAT_PCM;
...
mmres=waveOutOpen(&ghwo,uDeviceID,&wfx,dwCallback,dwCallbackInstance,fdwOpen);

Does that help at all?

1
David Heffernan On

The only problem I can see in the code you provided is that you did not fully initialise the struct. You did not initialise cbSize which in this instance must be set to 0.

Given that you are not allocating any extra data at the end of this struct, there's no need to allocate it in the heap.

It's entirely plausible that the problem lies in the other parameters that you pass to the function. We can't see any details of them, and therefore can't comment.