Skip to content

Commit

Permalink
Merge pull request #44 from FlavorJ/memallocfix-begin
Browse files Browse the repository at this point in the history
Fix memory leak in Adafruit_INA_219::begin() in subsequent calls
  • Loading branch information
ladyada authored Jan 5, 2021
2 parents bb528c4 + 0fe9d91 commit 0b752e3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
9 changes: 8 additions & 1 deletion Adafruit_INA219.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,20 @@ Adafruit_INA219::Adafruit_INA219(uint8_t addr) {
ina219_powerMultiplier_mW = 0.0f;
}

/*!
* @brief INA219 class destructor
*/
Adafruit_INA219::~Adafruit_INA219() { delete i2c_dev; }

/*!
* @brief Sets up the HW (defaults to 32V and 2A for calibration values)
* @param theWire the TwoWire object to use
* @return true: success false: Failed to start I2C
*/
bool Adafruit_INA219::begin(TwoWire *theWire) {
i2c_dev = new Adafruit_I2CDevice(ina219_i2caddr, theWire);
if (!i2c_dev) {
i2c_dev = new Adafruit_I2CDevice(ina219_i2caddr, theWire);
}

if (!i2c_dev->begin()) {
return false;
Expand Down
1 change: 1 addition & 0 deletions Adafruit_INA219.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ enum {
class Adafruit_INA219 {
public:
Adafruit_INA219(uint8_t addr = INA219_ADDRESS);
~Adafruit_INA219();
bool begin(TwoWire *theWire = &Wire);
void setCalibration_32V_2A();
void setCalibration_32V_1A();
Expand Down

0 comments on commit 0b752e3

Please sign in to comment.