简体   繁体   English

播放wav文件后,是否需要删除缓冲区?

[英]After playing wav file, do I need to delete the buffer?

I am trying to implement classes implementing the wav playing, as explained in this example . 我正在尝试实现实现wav播放的类,如本示例所述 The relevant code part is here : 相关的代码部分在这里:

/* Setup for conversion */
wav_cvt.buf = malloc(wav_len * wav_cvt.len_mult);
wav_cvt.len = wav_len;
memcpy(wav_cvt.buf, wav_buf, wav_len);

/* We can delete to original WAV data now */
SDL_FreeWAV(wav_buf);

/* And now we're ready to convert */
SDL_ConvertAudio(&wav_cvt);

When a wav file finishes playing (I am not going to play it again), do I need to free the memory buffer that is malloc()-ed above? 当wav文件结束播放时(我将不再播放),是否需要释放上面malloc()-ed的内存缓冲区? Or is it done automatically somewhere? 还是在某个地方自动完成?

No, nothing is done automatically. 不,什么都不会自动完成。 You must free it. 您必须释放它。

Remember that C (and anything of it's implementation) doesn't manage dynamic memory allocation automatically, whenever you have allocated some pieces of memory (mark the memory offset as USED), you should free() it when you are done to remark that offset as UNUSED. 请记住,C(及其任何实现)不会自动管理动态内存分配,每当您分配了一些内存(将内存偏移量标记为USED)时,完成标记该偏移量后就应该释放它()作为未使用。 But that's not MUST!!!. 但这不是必须的!

Any malloc is generally free 'd elsewhere by the same module. 通常,任何malloc都是由同一模块在其他位置free的。 I say generally because you may never intend to give the memory back for performance or persistence reasons. 我之所以这么说是因为您可能永远不会出于性能或持久性的原因而将内存退还给您。 Furthermore, memory allocations will be reclaimed by the operating system when the process terminates regardless You're not endangering the system. 此外,当进程终止时,无论您是否不危害系统,操作系统都将回收内存分配。

Since you malloc ed the buf, you should actually free it yourself. 由于您已 malloc了buf,因此实际上您应该自己free它。 Save SDL_FreeWav for wave buffers passed to you by SDL that you are done with (such as from SDL_LoadWav ). SDL_FreeWav保存为SDL传递给您的波形缓冲区(例如,从SDL_LoadWav传递给您)。

Internal to SDL_LoadWav , will be a malloc call by SDL. SDL_LoadWav内部,将是SDL的malloc调用。 SDL_FreeWav is a wrapper around the corresponding free . SDL_FreeWav是对应的free的包装。 This allocate/deallocate function pairing is common as some libraries may implement custom memory management routines that resemble or wrap malloc and free . 这种分配/解除分配功能配对很常见,因为某些库可能会实现类似于或包装mallocfree自定义内存管理例程。 They may even open up new heap contexts that are not accessible from the standard functions, and intended to be private. 它们甚至可能会打开无法从标准函数访问的新堆上下文,并且打算将其设为私有。 There's not even a requirement that the memory be allocated on a heap, but this is orthogonal to your question. 甚至没有要求在堆上分配内存,但这与您的问题正交。

It's likely that SDL_FreeWav is just a straight free , but when a library provides deallocation functions you should prefer those in case the behaviour differs. SDL_FreeWav可能只是直接的free ,但是当库提供释放函数时,如果行为不同, SDL_FreeWav使用那些函数。

When in doubt, always call the deallocation routine if you believe you're done with the memory resource. 如有疑问,如果您认为内存资源已用完,请始终调用释放例程。 Double free errors are noisy and generally generate stack traces that will let you quickly identify the problem. 双重free错误比较吵杂,通常会生成堆栈跟踪,使您可以快速识别问题。 Other libraries such as glib will usually have built-in diagnostics that will alert you to overzealous deallocation. 其他库(例如glib)通常具有内置的诊断程序,它们会警告您过度的重新分配。 Deallocating aggressively also aids in locating logical errors: If you think you're done with the memory, but some other part of the program isn't, the resource use will need to be re-examined. 积极地进行分配还有助于定位逻辑错误:如果您认为内存已用完,但是程序的其他部分还没有完成,则需要重新检查资源使用情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM