Browse Source

Fix device disposal

Fredrik Svantesson 6 years ago
parent
commit
61f6b01195
3 changed files with 35 additions and 23 deletions
  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 View File

169
             continue;
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
         // If the given device has any device_data, free it
189
         // If the given device has any device_data, free it
197
         {
191
         {
198
             free(curr_device->device_data);
192
             free(curr_device->device_data);
199
         }
193
         }
200
-        
194
+                
195
+        light_dispose_device(curr_device);    
196
+
201
         // Free the device
197
         // Free the device
202
         free(curr_device);
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
     return true;
201
     return true;
211
 }
202
 }
212
 
203
 

+ 18
- 0
src/light.c View File

473
     return success;
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
 bool light_free_enumerators(light_context_t *ctx)
487
 bool light_free_enumerators(light_context_t *ctx)
477
 {
488
 {
478
     bool success = true;
489
     bool success = true;
483
         {
494
         {
484
             success = false;
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
         free(curr_enumerator);
504
         free(curr_enumerator);
487
     }
505
     }
488
     
506
     

+ 3
- 0
src/light.h View File

135
 
135
 
136
 /* Returns the found device target, or null. Name should be enumerator/device/target */
136
 /* Returns the found device target, or null. Name should be enumerator/device/target */
137
 light_device_target_t* light_find_device_target(light_context_t *ctx, char const * name);
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);