diff --git a/src/base/theme.c b/src/base/theme.c index 58cb1ddab..444e3d082 100644 --- a/src/base/theme.c +++ b/src/base/theme.c @@ -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); } diff --git a/tests/theme_gen_test.cc b/tests/theme_gen_test.cc index 0b4e1c813..43f8ce544 100644 --- a/tests/theme_gen_test.cc +++ b/tests/theme_gen_test.cc @@ -252,30 +252,34 @@ TEST(ThemeGen, border) { const uint8_t* style_data = NULL; const char* str = ""; - 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 = ""; 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 = ""; 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 = ""; 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 = ""; theme = theme_xml_create(str);