[libdispatch-dev] Memory issues with libdispatch

Aldrin Martoq Ahumada aldrin.martoq at gmail.com
Sun Apr 29 23:09:06 PDT 2012

Hi, I have been testing sample code from the wiki and found that libdispatch is alloc'ing a lot of memory without releasing it, both in OS X (Lion 10.7.3, Xcode 4.3.2) and Linux (manually compiled and installed).

The code creates a SIGHUP signal source, with a handler that calls 100_000 writes to a logfile. The write to a log is queued in a serial queue.

The first run shows normal memory usage (8-9 MiB both Linux and OS X). After sending a "kill -1 <pid>" memory usage increases 1-20MiB every time I send the SIGHUP signal.

I have tried compiling with -fobj-arc and without it, but the result is the same. I removed the NSString usage in the br_log function, but the leak is not there: it is somewhere inside libdispatch.

I tried both valgrind and Xcode Instruments, but no leaks are found. Thanks in advance,

---- ini ----
//  main.m
//  test02
//  Created by Aldrin Martoq on 4/29/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

#include <unistd.h>
#import <Foundation/Foundation.h>
#import <dispatch/dispatch.h>

static FILE *log_file = NULL;
static NSString *log_filename = @"/tmp/br.log";
static dispatch_queue_t log_queue;

void br_log(NSString *format, ...) {
    @autoreleasepool {
        va_list ap;
        va_start(ap, format);
        NSString *s = [[NSString alloc] initWithFormat:format arguments:ap];
        if (log_file == NULL) {
            log_queue = dispatch_queue_create("cl.martoq.log_queue", NULL);
            log_file = fopen([log_filename cStringUsingEncoding:NSUTF8StringEncoding], "a");
            NSLog(@"Log file created: %@", log_filename);
        dispatch_async(log_queue, ^{
            @autoreleasepool {
                const char *c = [s cStringUsingEncoding:NSUTF8StringEncoding];
                fputs(c, log_file);
                fputs("\n", log_file);
                [s release];

void br_setup() {
    signal(SIGHUP, SIG_IGN);
    dispatch_source_t sig_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGHUP, 0, dispatch_get_main_queue());
    dispatch_source_set_event_handler(sig_src, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            printf("Caught SIGHUP\n");
            for (int i = 0; i < 100000; i++) {
                br_log(@"prueba: %d", i);

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"Hello, World!");
    return 0;
---- fini ----

Aldrin Martoq A.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/libdispatch-dev/attachments/20120430/f6b77427/attachment.html>

More information about the libdispatch-dev mailing list