瀏覽代碼

Fix device disposal

Fredrik Svantesson 6 年之前
父節點
當前提交
61f6b01195
共有 3 個文件被更改,包括 35 次插入23 次删除
  1. 14
    23
      src/impl/sysfs.c
  2. 18
    0
      src/light.c
  3. 3
    0
      src/light.h

+ 14
- 23
src/impl/sysfs.c 查看文件

@@ -169,27 +169,21 @@ bool impl_sysfs_free(light_device_enumerator_t *enumerator)
169 169
             continue;
170 170
         }
171 171
         
172
-        // If the given device has a targets array that isnt NULL, iterate through it to free the targets, then free the array
173
-        if(curr_device->targets != NULL)
172
+        for(uint64_t t = 0; t < curr_device->num_targets; t++)
174 173
         {
175
-            for(uint64_t t = 0; t < curr_device->num_targets; t++)
174
+            light_device_target_t *curr_target = curr_device->targets[t];
175
+            
176
+            if(curr_target == NULL)
176 177
             {
177
-                light_device_target_t *curr_target = curr_device->targets[t];
178
-                
179
-                if(curr_target == NULL)
180
-                {
181
-                    continue;
182
-                }
183
-                
184
-                if(curr_target->device_target_data != NULL)
185
-                {
186
-                    free(curr_target->device_target_data);
187
-                }
188
-                
189
-                free(curr_target);
178
+                continue;
190 179
             }
191 180
             
192
-            free(curr_device->targets);
181
+            if(curr_target->device_target_data != NULL)
182
+            {
183
+                free(curr_target->device_target_data);
184
+            }
185
+            
186
+            free(curr_target);
193 187
         }
194 188
         
195 189
         // If the given device has any device_data, free it
@@ -197,16 +191,13 @@ bool impl_sysfs_free(light_device_enumerator_t *enumerator)
197 191
         {
198 192
             free(curr_device->device_data);
199 193
         }
200
-        
194
+                
195
+        light_dispose_device(curr_device);    
196
+
201 197
         // Free the device
202 198
         free(curr_device);
203 199
     }
204 200
     
205
-    // Free the devices array
206
-    free(enumerator->devices);
207
-    enumerator->devices = NULL;
208
-    enumerator->num_devices = 0;
209
-    
210 201
     return true;
211 202
 }
212 203
 

+ 18
- 0
src/light.c 查看文件

@@ -473,6 +473,17 @@ bool light_init_enumerators(light_context_t *ctx)
473 473
     return success;
474 474
 }
475 475
 
476
+void light_dispose_device(light_device_t *device)
477
+{
478
+    if(device->targets != NULL)
479
+    {
480
+        free(device->targets);
481
+        device->targets = NULL;
482
+    }
483
+    
484
+    device->num_targets = 0;
485
+}
486
+
476 487
 bool light_free_enumerators(light_context_t *ctx)
477 488
 {
478 489
     bool success = true;
@@ -483,6 +494,13 @@ bool light_free_enumerators(light_context_t *ctx)
483 494
         {
484 495
             success = false;
485 496
         }
497
+        
498
+        if(curr_enumerator->devices != NULL)
499
+        {
500
+            free(curr_enumerator->devices);
501
+            curr_enumerator->devices = NULL;
502
+        }
503
+        
486 504
         free(curr_enumerator);
487 505
     }
488 506
     

+ 3
- 0
src/light.h 查看文件

@@ -135,3 +135,6 @@ bool light_split_target_path(char const * in_path, light_target_path_t *out_path
135 135
 
136 136
 /* Returns the found device target, or null. Name should be enumerator/device/target */
137 137
 light_device_target_t* light_find_device_target(light_context_t *ctx, char const * name);
138
+
139
+/* Frees any runtime-allocated data inside a device. Call this before you free a device. */
140
+void light_dispose_device(light_device_t *device);