Skip to content

Commit

Permalink
fix memory error when change theme
Browse files Browse the repository at this point in the history
  • Loading branch information
xianjimli committed Nov 18, 2024
1 parent e115dc9 commit 9433653
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
2024/11/18
* 完善 atomic(感谢兆坤提供补丁)
* 修复拼写错误(感谢兆坤提供补丁)
* fix memory error whene change theme.

2024/11/17
* 完善 TK_STRINGIZE(感谢兆坤提供补丁)
Expand Down
2 changes: 1 addition & 1 deletion src/awtk_global.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ ret_t tk_init_assets(void) {
theme_t* t = theme();
const char* iter_name = asset_info_get_name(iter);
if ((t == NULL || t->data == NULL) && tk_str_eq(iter_name, TK_DEFAULT_STYLE)) {
theme_set(theme_load_from_data(iter_name, iter->data, iter->size));
theme_set(theme_load_from_asset((asset_info_t*)iter));
}
break;
}
Expand Down
20 changes: 20 additions & 0 deletions src/base/theme.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ ret_t theme_destroy(theme_t* theme) {
TKMEM_FREE(theme->data);
}

if (theme->info != NULL) {
asset_info_unref(theme->info);
theme->info = NULL;
}

if (theme->theme_destroy != NULL) {
theme->theme_destroy(theme);
} else {
Expand Down Expand Up @@ -129,6 +134,21 @@ theme_t* theme_load_from_data(const char* name, const uint8_t* data, uint32_t si
}
}

theme_t* theme_load_from_asset(asset_info_t* info) {
theme_t* theme = NULL;
const char* name = NULL;
return_value_if_fail(info != NULL, NULL);

name = asset_info_get_name(info);
theme = theme_load_from_data(name, info->data, info->size);
return_value_if_fail(theme != NULL, NULL);

theme->info = info;
asset_info_ref(info);

return theme;
}

#ifndef WITHOUT_XML_STYLE
#include "theme_xml.inc"
#endif /*WITHOUT_XML_STYLE*/
Expand Down
12 changes: 12 additions & 0 deletions src/base/theme.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#ifndef TK_THEME_H
#define TK_THEME_H

#include "tkc/asset_info.h"
#include "base/theme_data.h"

BEGIN_C_DECLS
Expand All @@ -48,6 +49,7 @@ typedef ret_t (*theme_destroy_t)(theme_t* theme);
struct _theme_t {
const uint8_t* data;
bool_t need_free_data;
asset_info_t* info;

theme_foreach_t foreach;
theme_find_style_t find_style;
Expand Down Expand Up @@ -138,6 +140,16 @@ ret_t theme_destroy(theme_t* theme);
*/
theme_t* theme_load_from_data(const char* name, const uint8_t* data, uint32_t size);

/**
* @method theme_load_from_asset
* 加载窗体样式对象。
* @annotation ["constructor"]
* @param {asset_info_t*} info 资源对象。
*
* @return {theme_t*} 返回窗体样式对象。
*/
theme_t* theme_load_from_asset(asset_info_t* info);

#define TK_DEFAULT_STYLE "default"
#define THEME_DEFAULT_STYLE_TYPE "style_const"

Expand Down
2 changes: 1 addition & 1 deletion src/base/widget.c
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ ret_t widget_set_theme(widget_t* widget, const char* name) {

info = assets_manager_ref(am, ASSET_TYPE_STYLE, "default");
if (info != NULL) {
theme_set(theme_load_from_data(asset_info_get_name(info), info->data, info->size));
theme_set(theme_load_from_asset((asset_info_t*)info));
assets_manager_unref(assets_manager(), info);
}

Expand Down
5 changes: 2 additions & 3 deletions src/base/window_base.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ static ret_t window_base_load_default_theme_obj_impl(widget_t* widget, bool_t* u

if (window_base->default_res_theme != NULL) {
asset_info_t* res = (asset_info_t*)window_base->default_res_theme;
window_base->default_theme_obj =
theme_load_from_data(asset_info_get_name(res), res->data, res->size);
window_base->default_theme_obj = theme_load_from_asset(res);
}

if (window_base->default_theme_obj != NULL) {
Expand Down Expand Up @@ -152,7 +151,7 @@ static ret_t window_base_load_theme_obj_impl(widget_t* widget, bool_t* update_st

if (window_base->res_theme != NULL) {
asset_info_t* res = (asset_info_t*)window_base->res_theme;
window_base->theme_obj = theme_load_from_data(asset_info_get_name(res), res->data, res->size);
window_base->theme_obj = theme_load_from_asset(res);
}

if (window_base->theme_obj != NULL) {
Expand Down

0 comments on commit 9433653

Please sign in to comment.