Skip to content

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 🩹 Bug fix label Oct 19, 2023
@fpistm fpistm added this to the 2.7.0 milestone Oct 19, 2023
@fpistm fpistm merged commit ff1731f into stm32duino:main 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
fix 🩹 Bug fix
Projects
Development

Successfully merging this pull request may close these issues.

Possible memory leak in Wire.cpp
1 participant