fix memory leak

This commit is contained in:
lixianjing 2022-05-02 17:24:23 +08:00
parent b00bd67e83
commit 1a11c6cbdb
2 changed files with 17 additions and 3 deletions

View File

@ -24,6 +24,8 @@
#include "base/theme.h"
#include "tkc/buffer.h"
static theme_t* s_theme = NULL;
const uint8_t* theme_find_style(theme_t* theme, const char* widget_type, const char* name,
const char* widget_state) {
return_value_if_fail(theme != NULL, NULL);
@ -62,6 +64,10 @@ ret_t theme_set_theme_data(theme_t* theme, const uint8_t* data) {
ret_t theme_destroy(theme_t* theme) {
return_value_if_fail(theme != NULL, RET_BAD_PARAMS);
if (theme == s_theme) {
s_theme = NULL;
}
if (theme->need_free_data) {
TKMEM_FREE(theme->data);
@ -69,6 +75,8 @@ ret_t theme_destroy(theme_t* theme) {
if (theme->theme_destroy != NULL) {
theme->theme_destroy(theme);
} else {
TKMEM_FREE(theme);
}
return RET_OK;
@ -76,13 +84,15 @@ ret_t theme_destroy(theme_t* theme) {
/*global*/
static theme_t* s_theme = NULL;
theme_t* theme(void) {
return s_theme;
}
ret_t theme_set(theme_t* theme) {
if (theme == s_theme) {
return RET_OK;
}
if (s_theme != NULL) {
theme_destroy(s_theme);
}

View File

@ -252,30 +252,34 @@ TEST(ThemeGen, border) {
const uint8_t* style_data = NULL;
const char* str = "<button><style><normal border=\"left\" /></style></button>";
style_t* s = style_factory_create_style(NULL, theme_get_style_type(theme));
theme = theme_xml_create(str);
style_t* s = style_factory_create_style(NULL, theme_get_style_type(theme));
style_data = theme_find_style(theme, WIDGET_TYPE_BUTTON, TK_DEFAULT_STYLE, WIDGET_STATE_NORMAL);
ASSERT_EQ(style_set_style_data(s, style_data, WIDGET_STATE_NORMAL), RET_OK);
ASSERT_EQ(style_get_int(s, STYLE_ID_BORDER, 0), BORDER_LEFT);
theme_destroy(theme);
str = "<button><style><normal border=\"right\" /></style></button>";
theme = theme_xml_create(str);
style_data = theme_find_style(theme, WIDGET_TYPE_BUTTON, TK_DEFAULT_STYLE, WIDGET_STATE_NORMAL);
ASSERT_EQ(style_set_style_data(s, style_data, WIDGET_STATE_NORMAL), RET_OK);
ASSERT_EQ(style_get_int(s, STYLE_ID_BORDER, 0), BORDER_RIGHT);
theme_destroy(theme);
str = "<button><style><normal border=\"top\" /></style></button>";
theme = theme_xml_create(str);
style_data = theme_find_style(theme, WIDGET_TYPE_BUTTON, TK_DEFAULT_STYLE, WIDGET_STATE_NORMAL);
ASSERT_EQ(style_set_style_data(s, style_data, WIDGET_STATE_NORMAL), RET_OK);
ASSERT_EQ(style_get_int(s, STYLE_ID_BORDER, 0), BORDER_TOP);
theme_destroy(theme);
str = "<button><style><normal border=\"bottom\" /></style></button>";
theme = theme_xml_create(str);
style_data = theme_find_style(theme, WIDGET_TYPE_BUTTON, TK_DEFAULT_STYLE, WIDGET_STATE_NORMAL);
ASSERT_EQ(style_set_style_data(s, style_data, WIDGET_STATE_NORMAL), RET_OK);
ASSERT_EQ(style_get_int(s, STYLE_ID_BORDER, 0), BORDER_BOTTOM);
theme_destroy(theme);
str = "<button><style><normal border=\"all\" /></style></button>";
theme = theme_xml_create(str);