fmt2jpg seems to leak 128k of psram per call

ubercoder
Posts: 2
Joined: Thu Oct 03, 2024 8:38 pm

fmt2jpg seems to leak 128k of psram per call

Postby ubercoder » Thu Oct 03, 2024 9:01 pm

I'm running into a problem with my ESP32-cam.

I'm using it to capture an image which gets converted to raw rgb colour data for processing, before being converted back into a jpg.

I'm using the 'fmt2jpg' method from the 'esp_camera' library to do the conversion back into a jpg. It works, except that once its done its job, the available psram seems to have lost 128k.

My code stores the raw rgb pixel data in psram, but this gets fully released at the end of the process. I can account for all the psram that gets allocated & released, except for this 128k

My initial thought was that the 128k would be the size of the generated jpg image, but no, that's only around 20k, and anyway, this would be in the internal memory.

I plan to call this capture & process code repeatedly, but if it loses 128k each time, i'll only get 31 iterations ...

My code is basically this:

Code: Select all

void processImage(){

  camera_fb_t * initial_fb = NULL;
  initial_fb = esp_camera_fb_get();

	Serial.println("Start free psram = " + String(heap_caps_get_free_size(MALLOC_CAP_SPIRAM) / 1024) + "K");

  uint32_t ARRAY_LENGTH = initial_fb->width * initial_fb->height * 3;
  void *initial_ptrVal = NULL; 
  initial_ptrVal = heap_caps_malloc(ARRAY_LENGTH, MALLOC_CAP_SPIRAM);

	Serial.println("After allocation free psram = " + String(heap_caps_get_free_size(MALLOC_CAP_SPIRAM) / 1024) + "K");

  uint8_t *initial_rgb = (uint8_t *)initial_ptrVal; 
 
  bool jpeg_converted = fmt2rgb888(initial_fb->buf, initial_fb->len, PIXFORMAT_JPEG, initial_rgb);

.... do stuff with the raw RGB data ....

  size_t jpg_size = 0;
  uint8_t * jpg_buf = NULL;
  fmt2jpg(initial_rgb, ARRAY_LENGTH, 800, 600, PIXFORMAT_RGB888, 31, &jpg_buf, &jpg_size);
  heap_caps_free(initial_rgb);

.... save the JPG to the SD card ....

  jpg_buf = NULL;

	Serial.println("End free psram = " + String(heap_caps_get_free_size(MALLOC_CAP_SPIRAM) / 1024) + "K");

}
The Serial Monitor log output is:
Start free psram = 3999K
After allocation free psram = 2593K
End free psram = 3871K

I was expecting the method to end with the full 3999k of psram available for use next time...

Any thoughts?! TBH I'm extremely rusty as regards c++. I learned it 30years ago, but gave been writing Java since, so I wouldn't be surprised to discover I'mmaking an onvious error!

cheers

Paul

ESP_Sprite
Posts: 9833
Joined: Thu Nov 26, 2015 4:08 am

Re: fmt2jpg seems to leak 128k of psram per call

Postby ESP_Sprite » Fri Oct 04, 2024 7:55 am

Code: Select all

  jpg_buf = NULL;
That's not freeing the allocated jpeg buffer memory. You need to free(jpg_buf); instead.

ubercoder
Posts: 2
Joined: Thu Oct 03, 2024 8:38 pm

Re: fmt2jpg seems to leak 128k of psram per call

Postby ubercoder » Fri Oct 04, 2024 2:39 pm

Thanks ESP_Sprite ,

sorted now.

Its little things like this, which seem obvious in retrospect, relating to memory management that make me detest c & c++ with a passion...

cheers

Paul

ESP_Sprite
Posts: 9833
Joined: Thu Nov 26, 2015 4:08 am

Re: fmt2jpg seems to leak 128k of psram per call

Postby ESP_Sprite » Sat Oct 05, 2024 7:09 am

If that's an issue, we also support Rust on our chips, perhaps that's worth looking into?

Who is online

Users browsing this forum: No registered users and 78 guests