pnoltes commented on code in PR #796: URL: https://github.com/apache/celix/pull/796#discussion_r2276715282
########## documents/development/README.md: ########## @@ -381,37 +380,50 @@ For log levels use the following guidelines: - fatal: Use this level to report severe errors that prevent the program from continuing to run. After logging a fatal error, the program will typically terminate. -Example of error handling and logging: +Example of error handling and logging using auto pointers: ```c +typedef struct celix_foo { + celix_thread_mutex_t mutex; + bool mutexInitialized; + celix_array_list_t* list; + celix_long_hash_map_t* map; +} celix_foo_t; + +CELIX_DEFINE_AUTOPTR_CLEANUP_FUNC(celix_foo_t, celix_foo_destroy) + celix_foo_t* celix_foo_create(celix_log_helper_t* logHelper) { - celix_foo_t* foo = calloc(1, sizeof(*foo)); + celix_autoptr(celix_foo_t) foo = calloc(1, sizeof(*foo)); if (!foo) { - goto create_enomem_err; + celix_logHelper_log(logHelper, CELIX_LOG_LEVEL_ERROR, + "Error creating foo, out of memory"); + return NULL; } - - CELIX_GOTO_IF_ERR(create_mutex_err, celixThreadMutex_create(&foo->mutex, NULL)); - + + if (celixThreadMutex_create(&foo->mutex, NULL) == CELIX_SUCCESS) { + foo->mutexInitialized = true; + } else { + celix_logHelper_log(logHelper, CELIX_LOG_LEVEL_ERROR, + "Error creating mutex"); + return NULL; //foo cleaned up automatically + } + foo->list = celix_arrayList_create(); foo->map = celix_longHashMap_create(); - if (!foo->list || !foo->map) { - goto create_enomem_err; + if (!foo->list || !foo->map) { + celix_logHelper_log(logHelper, CELIX_LOG_LEVEL_ERROR, Review Comment: In the create function foo is created using a autoptr (`celix_autoptr(celix_foo_t) foo = calloc(1, sizeof(*foo));`). This should ensure that if there is a early return, `celix_foo_destroy` is called. I though this was cleaner, because you will reuse and also test corner cases of the destroy functions. I could update the example using: `celix_autofree celix_foo_t* foo = = calloc(1, sizeof(*foo));`. Same comment applies for https://github.com/apache/celix/pull/796#discussion_r2250234036, but it is good to note that because the create function is also using the destroy function the destroy function how now 2 usages (abnormal destroy and normal (user requested) destroy. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@celix.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org