Skip to content

Commit 819a9e6

Browse files
committed
Improved memory freeing
1 parent 512e16e commit 819a9e6

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

src/main.c

+33-33
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ int main(int argc, char* argv[])
651651
case 'd': {
652652
int parsed_correctly = get_two_ids(optarg, &selected_vendor_id, &selected_product_id);
653653
if (parsed_correctly == 1) {
654-
fprintf(stderr, "Usage: %s -d [vendorid:deviceid] (N = Number of connected devices - 1)\n", argv[0]);
654+
fprintf(stderr, "Usage: %s -d, --device [vendorid:deviceid]\n", argv[0]);
655655
return 1;
656656
}
657657
break;
@@ -930,7 +930,6 @@ int main(int argc, char* argv[])
930930
// We open connection to HID devices on demand
931931
hid_device* device_handle = NULL;
932932
char* hid_path = NULL;
933-
FeatureRequest* feature_requests[headset_available];
934933

935934
// Initialize signal handler for CTRL + C
936935
#ifdef _WIN32
@@ -961,10 +960,16 @@ int main(int argc, char* argv[])
961960
int numFeatures = sizeof(featureRequests) / sizeof(featureRequests[0]);
962961
assert(numFeatures == NUM_CAPABILITIES);
963962

963+
FeatureRequest* feature_requests[headset_available];
964+
for (int i = 0; i < headset_available; i++) {
965+
feature_requests[i] = memcpy(malloc(sizeof(featureRequests)), featureRequests, sizeof(featureRequests));
966+
devices_found[i].featureRequests = feature_requests[i];
967+
devices_found[i].size = numFeatures;
968+
}
969+
964970
// For specific output types, like YAML, we will do all actions - even when not specified - to aggreate all information
965971
if (output_format == OUTPUT_YAML || output_format == OUTPUT_JSON || output_format == OUTPUT_ENV) {
966972
for(int i=0; i<headset_available; i++) {
967-
feature_requests[i] = memcpy(malloc(sizeof(featureRequests)), featureRequests, sizeof(featureRequests));
968973
for(int j=0; j<numFeatures; j++){
969974
if (feature_requests[i][j].type == CAPABILITYTYPE_INFO && !feature_requests[i][j].should_process) {
970975
if ((devices_found[i].device->capabilities & B(feature_requests[i][j].cap)) == B(feature_requests[i][j].cap)) {
@@ -973,9 +978,6 @@ int main(int argc, char* argv[])
973978
}
974979
}
975980
}
976-
devices_found[i].featureRequests = feature_requests[i];
977-
devices_found[i].size = numFeatures;
978-
979981
terminate_device_hid(&device_handle, &hid_path);
980982
}
981983
}
@@ -1013,39 +1015,32 @@ int main(int argc, char* argv[])
10131015

10141016
if (battery_error != 0) {
10151017
printf("false\n");
1016-
return 1;
10171018
} else {
10181019
printf("true\n");
1019-
return 0;
10201020
}
1021-
}
1022-
1023-
do {
1024-
if (device_selected != NULL) {
1025-
FeatureRequest* deviceFeatureRequests = device_selected->featureRequests;
1026-
for (int i = 0; i < numFeatures; i++) {
1027-
if (deviceFeatureRequests[i].should_process) {
1028-
// Assuming handle_feature now returns FeatureResult
1029-
deviceFeatureRequests[i].result = handle_feature(device_selected->device, &device_handle, &hid_path, deviceFeatureRequests[i].cap, deviceFeatureRequests[i].param);
1030-
} else {
1031-
// Populate with a default "not processed" result
1032-
deviceFeatureRequests[i].result.status = FEATURE_NOT_PROCESSED;
1033-
deviceFeatureRequests[i].result.message = strdup("Not processed");
1034-
deviceFeatureRequests[i].result.value = 0;
1021+
} else{
1022+
do {
1023+
if (device_selected != NULL) {
1024+
FeatureRequest* deviceFeatureRequests = device_selected->featureRequests;
1025+
for (int i = 0; i < numFeatures; i++) {
1026+
if (deviceFeatureRequests[i].should_process) {
1027+
// Assuming handle_feature now returns FeatureResult
1028+
deviceFeatureRequests[i].result = handle_feature(device_selected->device, &device_handle, &hid_path, deviceFeatureRequests[i].cap, deviceFeatureRequests[i].param);
1029+
} else {
1030+
// Populate with a default "not processed" result
1031+
deviceFeatureRequests[i].result.status = FEATURE_NOT_PROCESSED;
1032+
deviceFeatureRequests[i].result.message = strdup("Not processed");
1033+
deviceFeatureRequests[i].result.value = 0;
1034+
}
10351035
}
10361036
}
1037-
}
10381037

1039-
output(devices_found, print_capabilities != -1, output_format);
1038+
output(devices_found, print_capabilities != -1, output_format);
10401039

1041-
if (follow)
1042-
sleep(follow_sec);
1040+
if (follow)
1041+
sleep(follow_sec);
10431042

1044-
} while (follow);
1045-
1046-
// Free memory from features
1047-
for (int i = 0; i < numFeatures; i++) {
1048-
//free(devices_found[device_index].featureRequests[i].result.message);
1043+
} while (follow);
10491044
}
10501045

10511046
if (equalizer != NULL) {
@@ -1054,8 +1049,13 @@ int main(int argc, char* argv[])
10541049
free(equalizer);
10551050

10561051
for (int i = 0; i < headset_available; i++) {
1057-
if (output_format != OUTPUT_STANDARD)
1058-
free(feature_requests[i]);
1052+
if (output_format != OUTPUT_STANDARD){
1053+
// Free memory from features
1054+
for (int j = 0; j < numFeatures; j++) {
1055+
free(devices_found[i].featureRequests[j].result.message);
1056+
}
1057+
}
1058+
free(devices_found[i].featureRequests);
10591059
free(devices_found[i].device);
10601060
}
10611061

0 commit comments

Comments
 (0)