If anything goes wrong during attach() it is handled with a 'goto fail' which calls sysctl_ctx_free(). But the sysctl context doesn't get initialized until very late in attach(), so almost any error just results in a segfault. Move the sysctl_ctx_init() call to the beginning of the attach() function, so that it is done before any errors can happen that will lead to freeing the context.