简体   繁体   中英

Why does malloc not return?

I have a thread running (OS X 10.10.5) that hangs with this stack trace. It's after about 20 hours of operation, probably related to a low memory condition. But, the question is, why doesn't malloc just return null? Why does the execution of the thread need to halt? The actual number of bytes requested is small (for a string operation on a path).

std::wstring finalPath = itsPath.substr(0,ls+1);

  1   operator new(unsigned long) + 37 (libc++abi.dylib + 124485) [0xa0339645] 2
   1   malloc + 29 (libsystem_malloc.dylib + 3997) [0xa1829f9d] 2
    1   malloc_zone_malloc + 116 (libsystem_malloc.dylib + 7243) [0xa182ac4b] 2
     1   default_zone_malloc + 3 (libsystem_malloc.dylib + 67909) [0xa1839945] 2
     *1   thread_exception_return + 0 (kernel + 632362) [0xffffff800029a62a] (runnable) 2

The purpose of the operation that fails is to clear out a temporary directory so the boot disk does not fill up with temporary files generated by the program. This process fails, the disk does indeed fill up, the thread halts (as in really halts, and the main thread is joined to it), and the process of deleting files off the disk stops. Also, the process has to be killed because the thread is halted. I don't know if you could exactly call this a bug since the process that fails is an attempt to prevent what actually happens.

Thank you for your insights.

You mention the disk is full, so the kernel probably is trying to swap out memory to disk and failing. In this situation, macOS suspends, rather than kills out-of-control processes. The idea is that you free some space and/or close down some other applications and then resume the suspended process, for example using kill -CONT <PID> .

By the sound of it, the real problem is that you're allocating too much memory, or leaking memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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