Skip to content

Commit 9995b23

Browse files
committed
fix: some memory leak bugs (#135)
1 parent 2a923a6 commit 9995b23

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

src/font/fontlibrary.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,11 @@ size_t LCUIFont_UpdateWeight( const int *font_ids,
300300
}
301301
}
302302
ids[count] = 0;
303-
if( new_font_ids ) {
303+
if( new_font_ids && count > 0 ) {
304304
*new_font_ids = ids;
305+
} else {
306+
* new_font_ids = NULL;
307+
free( ids );
305308
}
306309
return count;
307310
}
@@ -333,8 +336,11 @@ size_t LCUIFont_UpdateStyle( const int *font_ids,
333336
}
334337
}
335338
ids[count] = 0;
336-
if( new_font_ids ) {
339+
if( new_font_ids && count > 0 ) {
337340
*new_font_ids = ids;
341+
} else {
342+
*new_font_ids = NULL;
343+
free( ids );
338344
}
339345
return count;
340346
}

src/font/textlayer.c

+14-1
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,25 @@ static void TextRowList_Destroy( TextRowList list )
321321
list->rows = NULL;
322322
}
323323

324+
static void OnDestroyTextStyle( void *data )
325+
{
326+
TextStyle_Destroy( data );
327+
free( data );
328+
}
329+
330+
static void TextLayer_DestroyStyleCache( LCUI_TextLayer layer )
331+
{
332+
LinkedList_Clear( &layer->style_cache, OnDestroyTextStyle );
333+
}
334+
324335
/** 销毁TextLayer */
325336
void TextLayer_Destroy( LCUI_TextLayer layer )
326337
{
338+
327339
RectList_Clear( &layer->dirty_rect );
328340
TextStyle_Destroy( &layer->text_style );
329341
TextRowList_Destroy( &layer->text_rows );
342+
TextLayer_DestroyStyleCache( layer );
330343
Graph_Free( &layer->graph );
331344
free( layer );
332345
}
@@ -531,8 +544,8 @@ void TextLayer_ClearText( LCUI_TextLayer layer )
531544
layer->length = 0;
532545
layer->insert_x = 0;
533546
layer->insert_y = 0;
547+
TextLayer_DestroyStyleCache( layer );
534548
TextRowList_Destroy( &layer->text_rows );
535-
LinkedList_Clear( &layer->style_cache, (FuncPtr)TextStyle_Destroy );
536549
TextRowList_InsertNewRow( &layer->text_rows, 0 );
537550
layer->task.redraw_all = TRUE;
538551
}

src/font/textstyle.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ void TextStyle_Init( LCUI_TextStyle data )
5555
data->has_style = FALSE;
5656
data->has_weight = FALSE;
5757
data->has_family = FALSE;
58+
data->has_pixel_size = FALSE;
5859
data->has_back_color = FALSE;
5960
data->has_fore_color = FALSE;
6061
data->font_ids = NULL;
@@ -273,15 +274,16 @@ LCUI_TextStyle StyleTags_GetTextStyle( LinkedList *tags )
273274
/** 将指定标签的样式数据从队列中删除,只删除队列尾部第一个匹配的标签 */
274275
static void StyleTags_Delete( LinkedList *tags, int id )
275276
{
276-
LCUI_TextStyleTag *p;
277+
LCUI_TextStyleTag *tag;
277278
LinkedListNode *node;
278279
DEBUG_MSG( "delete start, total tag: %d\n", total );
279280
if( tags->length <= 0 ) {
280281
return;
281282
}
282283
for( LinkedList_Each( node, tags ) ) {
283-
p = (LCUI_TextStyleTag*)node->data;
284-
if( p->id == id ) {
284+
tag = node->data;
285+
if( tag->id == id ) {
286+
free( tag );
285287
LinkedList_DeleteNode( tags, node );
286288
break;
287289
}
@@ -508,7 +510,7 @@ const wchar_t *StyleTags_GetStart( LinkedList *tags, const wchar_t *str )
508510
q = ScanStyleTagData( str, tag );
509511
if( q ) {
510512
/* 将标签样式数据加入队列 */
511-
LinkedList_Append( tags, tag );
513+
LinkedList_Insert( tags, 0, tag );
512514
} else {
513515
free( tag );
514516
}

src/gui/widget/textedit.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,8 @@ static void TextEdit_OnTask( LCUI_Widget widget )
415415
if( edit->tasks[TASK_SET_TEXT] ) {
416416
LinkedList blocks;
417417
LinkedListNode *node;
418-
LCUI_WidgetEventRec ev;
418+
LCUI_WidgetEventRec ev = { 0 };
419+
419420
LinkedList_Init( &blocks );
420421
LCUIMutex_Lock( &edit->mutex );
421422
LinkedList_Concat( &blocks, &edit->text_blocks );

src/platform/linux/linux_x11display.c

+2
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ static void OnDestroySurface( void *data )
237237
{
238238
LCUI_Surface s = data;
239239
X11Surface_ClearTasks( s );
240+
XDestroyImage( s->ximage );
241+
XFreeGC( x11.app->display, s->gc );
240242
free( s );
241243
}
242244

0 commit comments

Comments
 (0)