Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Wire): avoid memory leaks #2148

Merged
merged 1 commit into from
Oct 19, 2023
Merged

Conversation

fpistm
Copy link
Member

@fpistm fpistm commented Oct 19, 2023

add destructor to call end().
Fixes #2142

Tested with this example:
#include <Wire.h>
#include <malloc.h>

extern "C" char *sbrk(int i);
/* Use linker definition */
extern char _end;
extern char _sdata;
extern char _estack;
extern char _Min_Stack_Size;

static char *ramstart = &_sdata;
static char *ramend = &_estack;
static char *minSP = (char *)(ramend - &_Min_Stack_Size);

void display_mallinfo(void) {
  char *heapend = (char *)sbrk(0);
  char *stack_ptr = (char *)__get_MSP();
  struct mallinfo mi = mallinfo();

  Serial.print("Total non-mmapped bytes (arena):       ");
  Serial.println(mi.arena);
  Serial.print("# of free chunks (ordblks):            ");
  Serial.println(mi.ordblks);
  Serial.print("# of free fastbin blocks (smblks):     ");
  Serial.println(mi.smblks);
  Serial.print("# of mapped regions (hblks):           ");
  Serial.println(mi.hblks);
  Serial.print("Bytes in mapped regions (hblkhd):      ");
  Serial.println(mi.hblkhd);
  Serial.print("Max. total allocated space (usmblks):  ");
  Serial.println(mi.usmblks);
  Serial.print("Free bytes held in fastbins (fsmblks): ");
  Serial.println(mi.fsmblks);
  Serial.print("Total allocated space (uordblks):      ");
  Serial.println(mi.uordblks);
  Serial.print("Total free space (fordblks):           ");
  Serial.println(mi.fordblks);
  Serial.print("Topmost releasable block (keepcost):   ");
  Serial.println(mi.keepcost);

  Serial.print("RAM Start at:       0x");
  Serial.println((unsigned long)ramstart, HEX);
  Serial.print("Data/Bss end at:    0x");
  Serial.println((unsigned long)&_end, HEX);
  Serial.print("Heap end at:        0x");
  Serial.println((unsigned long)heapend, HEX);
  Serial.print("Stack Ptr end at:   0x");
  Serial.println((unsigned long)stack_ptr, HEX);
  Serial.print("RAM End at:         0x");
  Serial.println((unsigned long)ramend, HEX);

  Serial.print("Heap RAM Used:      ");
  Serial.println(mi.uordblks);
  Serial.print("Program RAM Used:   ");
  Serial.println(&_end - ramstart);
  Serial.print("Stack RAM Used:     ");
  Serial.println(ramend - stack_ptr);
  Serial.print("Estimated Free RAM: ");
  Serial.println(((stack_ptr < minSP) ? stack_ptr : minSP) - heapend + mi.fordblks);
}

void setup() {
  // initialize serial communication at 115200 bits per second:
  Serial.begin(115200);
  delay(1000);
}
#define DATA_SIZE WIRE_MAX_TX_BUFF_LENGTH
const uint8_t data[DATA_SIZE] = { 0xFF };
void loop() {

  Serial.println("============== Before allocating blocks ==============");
  display_mallinfo();
  // Instantiate Wire
  TwoWire Wire1 = TwoWire();
  Wire1.begin();
  Wire1.beginTransmission(0x38);
  Wire1.write(data, DATA_SIZE);

  Serial.println("============== After allocating blocks ==============");
  display_mallinfo();
  delay(1000);
}

add destructor to call end().
Fixes stm32duino#2142

Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
@fpistm fpistm added the Fix label Oct 19, 2023
@fpistm fpistm added this to the 2.7.0 milestone Oct 19, 2023
@fpistm fpistm added this to In progress in STM32 core based on ST HAL via automation Oct 19, 2023
@fpistm fpistm merged commit ff1731f into stm32duino:main Oct 19, 2023
22 checks passed
STM32 core based on ST HAL automation moved this from In progress to Done Oct 19, 2023
@fpistm fpistm deleted the Wire_destructor branch October 19, 2023 14:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Development

Successfully merging this pull request may close these issues.

Possible memory leak in Wire.cpp
1 participant