Skip to content

Commit

Permalink
fix memory leak, and fix dispatch_queue_id_prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
xiazhvera committed Jan 23, 2025
1 parent c2dab8d commit 4fadfee
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions source/darwin/dispatch_queue_event_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ static void s_dispatch_event_loop_destroy(void *context) {

static const char AWS_LITERAL_APPLE_DISPATCH_QUEUE_ID_PREFIX[] = "com.amazonaws.commonruntime.eventloop.";
static const size_t AWS_IO_APPLE_DISPATCH_QUEUE_ID_PREFIX_LENGTH =
AWS_ARRAY_SIZE(AWS_LITERAL_APPLE_DISPATCH_QUEUE_ID_PREFIX);
AWS_ARRAY_SIZE(AWS_LITERAL_APPLE_DISPATCH_QUEUE_ID_PREFIX) - 1; // remove string terminator
static const size_t AWS_IO_APPLE_DISPATCH_QUEUE_ID_LENGTH =
AWS_IO_APPLE_DISPATCH_QUEUE_ID_PREFIX_LENGTH + AWS_UUID_STR_LEN;
/**
Expand Down Expand Up @@ -325,19 +325,27 @@ struct aws_event_loop *aws_event_loop_new_with_dispatch_queue(
aws_linked_list_init(&dispatch_loop->synced_data.cross_thread_tasks);

struct dispatch_loop_context *context = aws_mem_calloc(alloc, 1, sizeof(struct dispatch_loop_context));
aws_ref_count_init(&context->ref_count, context, s_dispatch_loop_context_destroy);
context->allocator = alloc;
aws_mutex_init(&context->scheduling_state.services_lock);

if (aws_priority_queue_init_dynamic(
&context->scheduling_state.scheduled_services,
alloc,
DEFAULT_QUEUE_SIZE,
sizeof(struct scheduled_service_entry *),
&s_compare_timestamps)) {
AWS_LOGF_INFO(
AWS_LS_IO_EVENT_LOOP,
"id=%p: priority queue creation failed, clean up the context: %s",
(void *)loop,
dispatch_queue_id);
aws_mem_release(alloc, context);
goto clean_up;
};

aws_ref_count_init(&context->ref_count, context, s_dispatch_loop_context_destroy);
context->allocator = alloc;

aws_mutex_init(&context->scheduling_state.services_lock);

aws_rw_lock_init(&context->lock);
context->io_dispatch_loop = dispatch_loop;
dispatch_loop->context = context;
Expand Down Expand Up @@ -595,6 +603,9 @@ static void s_schedule_task_common(struct aws_event_loop *event_loop, struct aws
struct dispatch_loop *dispatch_loop = event_loop->impl_data;

s_rlock_dispatch_loop_context(dispatch_loop->context);
if (dispatch_loop->context->io_dispatch_loop == NULL) {
goto schedule_task_common_cleanup;
}
s_lock_cross_thread_data(dispatch_loop);
task->timestamp = run_at_nanos;

Expand Down Expand Up @@ -630,6 +641,7 @@ static void s_schedule_task_common(struct aws_event_loop *event_loop, struct aws
}

s_unlock_cross_thread_data(dispatch_loop);
schedule_task_common_cleanup:
s_runlock_dispatch_loop_context(dispatch_loop->context);
}

Expand Down

0 comments on commit 4fadfee

Please sign in to comment.