【问题标题】:Heap corruption detected - iPhone 5S only检测到堆损坏 - 仅限 iPhone 5S
【发布时间】:2013-11-29 10:48:04
【问题描述】:

我正在开发一个监听频率/音高的应用程序,它在 iPhone4s、模拟器和其他设备上运行良好,但在 iPhone 5S 上运行良好。这是我收到的信息:

malloc: *** error for object 0x178203a00: Heap corruption detected, free list canary is damaged

有什么建议我应该从哪里开始研究?

谢谢!

【问题讨论】:

  • 损坏可能出现在刚刚在 5s 上检测到的设备上。搜索可用于查找问题的 malloc_debug 库。

标签: ios objective-c audio core-audio


【解决方案1】:

iPhone 5s 具有 arm64/64 位 CPU。检查所有试图将 64 位指针(和其他值)存储为 32 位 C 数据类型的分析编译器警告。

还要确保您的所有音频代码参数传递、对象消息传递和手动内存管理代码都是线程安全的,并且满足所有实时要求。

【讨论】:

    【解决方案2】:

    如果它对任何人有帮助,我遇到的问题与上述完全相同。

    在我的特殊情况下,原因是 ARM64 上的 pthread_create(pthread_t* thread, ...) 在线程启动后的某个时间将值放入 *thread。在 OSX、ARM32 和模拟器上,在调用 start_routine 之前,它一直在填充这个值。

    如果我在写入该值之前在正在运行的线程中执行了 pthread_detach 操作(即使使用 pthread_self() 来获取当前 thread_t),我最终会收到堆损坏消息。

    我在我的线程调度程序中添加了一个小循环,等待该值被填充——之后堆错误就消失了。不要忘记“易失性”!

    重组代码可能是解决此问题的更好方法——这取决于您的情况。 (我在我编写的单元测试中注意到了这一点,我没有在任何“真实”代码上遇到这个问题)

    【讨论】:

      【解决方案3】:

      同样的问题。但我的情况是我 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\0".
               *
               * 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\0" would be written as output[0] = '0', output[1] = 'A' and
               * output[2] = '\0'.
               *
               * "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;
      
      

      【讨论】:

        猜你喜欢
        • 2011-07-24
        • 1970-01-01
        • 2018-03-26
        • 1970-01-01
        • 2011-08-20
        • 1970-01-01
        • 2020-08-02
        • 1970-01-01
        • 2013-07-06
        相关资源
        最近更新 更多