PengZheng commented on code in PR #796:
URL: https://github.com/apache/celix/pull/796#discussion_r2278098081


##########
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:
   Ah, yes. Note that there is an implicit tradeoff: the half-baked object 
needs to memorize which resources have been initialized.
   And this is asymmetric to the C++ case: the ctor does not rely on its own 
dtor to do cleanup job, but it does rely on its members' dtors.
   
   But I am OK with this approach. 



-- 
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

Reply via email to