Memory management of display_text and font_name

Please provide complete information as applicable to your setup.

• Hardware Platform :Jetson
• DeepStream Version: 6.0.1
• question

I have written a utility function to add text to the osd like:

void DrawingPrimitives::DrawText(NvDsFrameMeta *frame_meta, NvDsBatchMeta *batch_meta,  NvDsDisplayMeta **disp_meta, string text, int x, int y, int fontsize, string font_name, float r, float g, float b, float a)
{
  if( !(*disp_meta))
    (*disp_meta) = acquire_display_meta_from_pool(batch_meta);

  int index = (*disp_meta)->num_labels;

  NvOSD_TextParams *text_params = &(*disp_meta)->text_params[index]; 
  
  text_params->display_text = (char *)g_malloc0(text.length() + 1);
  strcpy(text_params->display_text, text.c_str());
  
  text_params->x_offset = x;
  text_params->y_offset = y;

  text_params->font_params.font_name = (char *)g_malloc0(font_name.length() + 1);
  strcpy(text_params->font_params.font_name, font_name.c_str());
  text_params->font_params.font_size = fontsize;

  text_params->font_params.font_color.red = r;
  text_params->font_params.font_color.green = g;
  text_params->font_params.font_color.blue = b;
  text_params->font_params.font_color.alpha = a;

  text_params->set_bg_clr = 1;
  text_params->text_bg_clr.red = 0.5;
  text_params->text_bg_clr.green = 0.0;
  text_params->text_bg_clr.blue = 0.5;
  text_params->text_bg_clr.alpha = 0;

  (*disp_meta)->num_labels++;

  if ((*disp_meta)->num_labels == MAX_ELEMENTS_IN_DISPLAY_META)
  {
    nvds_add_display_meta_to_frame(frame_meta, (*disp_meta));
    (*disp_meta) = acquire_display_meta_from_pool(batch_meta);
  }
}

but i am worried about

  text_params->display_text = (char *)g_malloc0(text.length() + 1);
and
  text_params->font_params.font_name = (char *)g_malloc0(font_name.length() + 1);

The only examples i see are either where a display_text is added to (concatenated) or it is set with a static value
display_text = “My text”

I would like to set a dynamic text / font

I tried:

void release_text_data(void *data, void *user_data)
{
  NvDsDisplayMeta *srcMeta = (NvDsDisplayMeta *)data;
  if (srcMeta != nullptr)
  {
    for (int i = 0; i < srcMeta->num_labels; i++)
    {
      NvOSD_TextParams *text_params = &srcMeta->text_params[i];
      if (text_params)
      {
        cout << "++releasing text_params" << endl;
        g_free(text_params->display_text);
        g_free(text_params->font_params.font_name);
        cout << "--releasing text_params" << endl;
      }
    }
  }
}

and

  (*disp_meta)->base_meta.release_func = release_text_data;

But that crashes (eventually)

how is this memory released ? Do i need to release the memory or is that done by the nvosd ? Is this documented somewhere ? Is there an example I can look at?

We will manage all the metadata memory in a memory pool. The memory is automatically released at the right time.
You can refer to the NvDsMetaPool.

Ah good. so i don’t have to worry about releasing that memory once handed over ?

Yes. If you get metadata in the following way acquire_display_meta_from_pool, it does not need to be worried about that.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.