Skip to content

Commit

Permalink
[SensorBHI260AP]Fix possible memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisxhe committed Jan 24, 2025
1 parent 7bc3c4f commit c384173
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/SensorBHI260AP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -685,10 +685,12 @@ BoschSensorInfo SensorBHI260AP::getSensorInfo()
if (bhy2_get_error_value(&sensorInfo.sensor_error, _bhy2.get()) != BHY2_OK) {
log_e("bhy2_get_error_value failed!");
}
for (uint8_t i = 0; i < BHY2_SENSOR_ID_MAX; i++) {
if (bhy2_is_sensor_available(i, _bhy2.get())) {
if (bhy2_get_sensor_info(i, &sensorInfo.info[i], _bhy2.get()) != BHY2_OK) {
log_e("bhy2_get_sensor_info [%u] failed!", i);
if (sensorInfo.info) {
for (uint8_t i = 0; i < BHY2_SENSOR_ID_MAX; i++) {
if (bhy2_is_sensor_available(i, _bhy2.get())) {
if (bhy2_get_sensor_info(i, &sensorInfo.info[i], _bhy2.get()) != BHY2_OK) {
log_e("bhy2_get_sensor_info [%u] failed!", i);
}
}
}
}
Expand Down
94 changes: 93 additions & 1 deletion src/bosch/BoschSensorInfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,93 @@ class BoschSensorInfo

~BoschSensorInfo()
{
free(info);
if (info) {
free(info);
}
}

BoschSensorInfo(const BoschSensorInfo &other)
: kernel_version(other.kernel_version),
user_version(other.user_version),
rom_version(other.rom_version),
product_id(other.product_id),
host_status(other.host_status),
feat_status(other.feat_status),
boot_status(other.boot_status),
sensor_error(other.sensor_error),
dev(other.dev),
info(nullptr)
{
if (other.info) {
info = (struct bhy2_sensor_info *)calloc(BHY2_SENSOR_ID_MAX, sizeof(struct bhy2_sensor_info));
for (int i = 0; i < BHY2_SENSOR_ID_MAX; ++i) {
info[i] = other.info[i];
}
}
}

BoschSensorInfo &operator=(const BoschSensorInfo &other)
{
if (this != &other) {
if (info) {
free(info);
}
kernel_version = other.kernel_version;
user_version = other.user_version;
rom_version = other.rom_version;
product_id = other.product_id;
host_status = other.host_status;
feat_status = other.feat_status;
boot_status = other.boot_status;
sensor_error = other.sensor_error;
dev = other.dev;
info = nullptr;
if (other.info) {
info = (struct bhy2_sensor_info *)calloc(BHY2_SENSOR_ID_MAX, sizeof(struct bhy2_sensor_info));
for (int i = 0; i < BHY2_SENSOR_ID_MAX; ++i) {
info[i] = other.info[i];
}
}
}
return *this;
}

BoschSensorInfo(BoschSensorInfo &&other) noexcept
: kernel_version(other.kernel_version),
user_version(other.user_version),
rom_version(other.rom_version),
product_id(other.product_id),
host_status(other.host_status),
feat_status(other.feat_status),
boot_status(other.boot_status),
sensor_error(other.sensor_error),
dev(other.dev),
info(other.info)
{
other.info = nullptr;
other.dev = nullptr;
}

BoschSensorInfo &operator=(BoschSensorInfo &&other) noexcept
{
if (this != &other) {
if (info) {
free(info);
}
kernel_version = other.kernel_version;
user_version = other.user_version;
rom_version = other.rom_version;
product_id = other.product_id;
host_status = other.host_status;
feat_status = other.feat_status;
boot_status = other.boot_status;
sensor_error = other.sensor_error;
dev = other.dev;
info = other.info;
other.info = nullptr;
other.dev = nullptr;
}
return *this;
}

#ifdef ARDUINO
Expand All @@ -69,6 +155,9 @@ class BoschSensorInfo
if (!dev) {
return;
}
if (!info) {
return;
}
if (feat_status & BHY2_FEAT_STATUS_OPEN_RTOS_MSK) {
stream.printf("Virtual sensor list.\n");
stream.printf("Sensor ID | Sensor Name | ID | Ver | Min rate | Max rate |\n");
Expand Down Expand Up @@ -138,6 +227,9 @@ class BoschSensorInfo
if (!dev) {
return;
}
if (!info) {
return;
}
if (feat_status & BHY2_FEAT_STATUS_OPEN_RTOS_MSK) {
printf("Virtual sensor list.\n");
printf("Sensor ID | Sensor Name | ID | Ver | Min rate | Max rate |\n");
Expand Down

0 comments on commit c384173

Please sign in to comment.