uiBut and Images

#1

I am working a lot with uiDefBut and its variants, the docs say that when a block is defined with the same name as previously it deletes itself releasing all buttons as well.

What happens when buttons use images like the splash screen dialog ? Do they images get released too ?

I am using basically the poin variable of the button to load the image to display my own icons because I create an icon heavy interface. I know that Blender is releasing it resources on shut down and I load imaged the same way the splash dialogue does, although I dont use a template like it does its a direct
load. I am using this approach to design my own custom right click menu which will be icon based.

Is there anything I should be aware of ?

I paste how I create the buttons and load the images/icons. Morph here is a struct I use to represent an icon button, nothing special.

/* creates a button using the filename of a image file as icon ,
 an operator that is triggered when button is clicked, position x,y
 width, height and text for tooltip */
 static hec_morph *hec_create_morph(uiBlock *block, hec_morph *morph)
 {
	 //printf("calling EphMorph\n");
	char image_filepath[256];
	hec_find_image_path(morph->texture_path,image_filepath);
	morph->ibuf = aci_if->bl_IMB_loadiffname(image_filepath, IB_rect, NULL);
	if(morph->type=="operator")
	{
		morph->but = aci_if->bl_uiDefIconButO(block, UI_BTYPE_IMAGE, morph->operator_name, WM_OP_INVOKE_DEFAULT, ICON_NONE, morph->x, morph->y, morph->width, morph->height, "");
		morph->but->poin = (char *)morph->ibuf;
	}
	if(morph->type=="default")
	{
		morph->but = aci_if->bl_uiDefBut(block, UI_BTYPE_IMAGE, 0, "", morph->x, morph->y, morph->width, morph->height, (char *)morph->ibuf,0.0,0.0,0,0, "");
		morph->but->poin = (char *)morph->ibuf;	
	}
	//but->tip = "";
	return morph;
 };

The code works fine but I am a bit worried about potential memory leaks

0 Likes

#2

to answer my own question yes, the imBuf of a button is released , because on refresh the old block that the button belongs too is released and replaced with a new block and that means the image referenced by the button is released too. There is a possibility to keep the image using the IMB_dupImBuf(ImBuf *ibuf) which will duplicate the image buffer and keep it around as long as its not referenced by a button or other ui element that releases its image with a call to void IMB_freeImBuf(ImBuf *ibuf) in case of buttons called via static void ui_but_free(const bContext *C, uiBut *but)

0 Likes