我正在开发一款监听频率/音高的应用程序,它在iPhone4s、模拟器和其他应用程序上运行良好,但在iPhone 5S上运行不佳。这是我得到的信息:
malloc: *** error for object 0x178203a00: Heap corruption detected, free list canary is damaged
有什么建议我应该从哪里开始研究吗?
谢谢!
iPhone 5s具有arm64/64位CPU。检查所有试图将64位指针(和其他值)存储到32位C数据类型的分析编译器警告。
还要确保所有音频代码参数传递、对象消息传递和手动内存管理代码都是线程安全的,并且满足所有实时要求。
如果它能帮助任何人,我遇到了与上述完全相同的问题。
在我的特殊情况下,原因是ARM64上的pthread_create(pthread_t*thread,…)在线程启动后的某个时间将值放入*thread。在OSX、ARM32和模拟器上,在调用start_routine之前,它一直在填充这个值。
如果在写入该值之前在运行的线程中执行pthread_detach操作(即使使用pthread_self()获取当前线程_t),我也会收到堆损坏消息。
我在线程调度程序中添加了一个小循环,等待该值被填充——之后堆错误就消失了。别忘了"易失性"!
重组代码可能是解决这个问题的更好方法——这取决于您的情况。(我在我写的一个单元测试中注意到了这一点,我在任何"真实"代码上都没有遇到这个问题)
同样的问题。但我的情况是我的malloc 10Byte内存,但我尝试使用20Byte。然后它堆积腐败。
@@ -64,7 +64,7 @@ char* bytesToHex(char* buf, int size) {
* be converted to two hex characters, also add an extra space for the terminating
* null byte.
* [size] is the size of the buf array */
- int len = (size * 2) + 1;
+ int len = (size * 3) + 1;
char* output = (char*)malloc(len * sizeof(char));
memset(output, 0, len);
/* pointer to the first item (0 index) of the output array */
char *ptr = &output[0];
int i;
for (i = 0; i < size; i++) {
/* "sprintf" converts each byte in the "buf" array into a 2 hex string
* characters appended with a null byte, for example 10 => "0A ".
*
* This string would then be added to the output array starting from the
* position pointed at by "ptr". For example if "ptr" is pointing at the 0
* index then "0A " would be written as output[0] = '0', output[1] = 'A' and
* output[2] = ' '.
*
* "sprintf" returns the number of chars in its output excluding the null
* byte, in our case this would be 2. So we move the "ptr" location two
* steps ahead so that the next hex string would be written at the new
* location, overriding the null byte from the previous hex string.
*
* We don't need to add a terminating null byte because it's been already
* added for us from the last hex string. */
ptr += sprintf(ptr, "%02X ", buf[i] & 0xFF);
}
return output;