-1

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

lkanab
  • 924
  • 1
  • 7
  • 20

2 Answers2

2

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
1

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?

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
  • I can, but that doesn't answer my question. Thanks anyway – lkanab Aug 19 '18 at 06:28
  • Then, as @DavidHeffernan says, the problem must lie elsewhere. Also, *please*, post text and not images. Thanks. – Paul Sanders Aug 19 '18 at 07:04
  • The API can be used with additional data at the end of the struct, which is why the documentation describes freeing the struct. In that scenario it would be common to allocate on the heap. Of course that is not the scenario here. – David Heffernan Aug 19 '18 at 08:09