From abb5417819cb59eb39999e6630f1ae9324667465 Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Tue, 18 Mar 2014 15:43:42 +0400 Subject: [PATCH] resource cleanup fixed --- examples/example1/main.d | 69 ++++++++++++++++++---------------- src/dlangui/graphics/drawbuf.d | 6 +++ src/dlangui/graphics/ftfonts.d | 7 ++-- src/dlangui/graphics/images.d | 13 +++++++ src/dlangui/widgets/styles.d | 10 +++++ 5 files changed, 70 insertions(+), 35 deletions(-) diff --git a/examples/example1/main.d b/examples/example1/main.d index ca25b42e..8d0713f5 100644 --- a/examples/example1/main.d +++ b/examples/example1/main.d @@ -40,45 +40,50 @@ extern (C) int UIAppMain(string[] args) { // create window Window window = Platform.instance().createWindow("My Window", null); - LinearLayout layout = new LinearLayout(); - layout.addChild((new TextWidget()).textColor(0x00802000).text("Text widget 0")); - layout.addChild((new TextWidget()).textColor(0x40FF4000).text("Text widget")); - layout.addChild((new Button("BTN1")).text("Button1")); //.textColor(0x40FF4000) - - + static if (false) { + LinearLayout layout = new LinearLayout(); + layout.addChild((new TextWidget()).textColor(0x00802000).text("Text widget 0")); + layout.addChild((new TextWidget()).textColor(0x40FF4000).text("Text widget")); + layout.addChild((new Button("BTN1")).text("Button1")); //.textColor(0x40FF4000) + + + - LinearLayout hlayout = new HorizontalLayout(); - //hlayout.addChild((new Button()).text("<<")); //.textColor(0x40FF4000) - hlayout.addChild((new TextWidget()).text("Several").alignment(Align.Center)); - hlayout.addChild((new ImageWidget()).drawableId("exit").padding(Rect(5,5,5,5)).alignment(Align.Center)); - hlayout.addChild((new TextWidget()).text("items").alignment(Align.Center)); - hlayout.addChild((new ImageWidget()).drawableId("exit").padding(Rect(5,5,5,5)).alignment(Align.Center)); - hlayout.addChild((new TextWidget()).text("in horizontal layout")); - hlayout.addChild((new ImageWidget()).drawableId("exit").padding(Rect(5,5,5,5)).alignment(Align.Center)); - //hlayout.addChild((new Button()).text(">>")); //.textColor(0x40FF4000) - hlayout.backgroundColor = 0x8080C0; - layout.addChild(hlayout); + LinearLayout hlayout = new HorizontalLayout(); + //hlayout.addChild((new Button()).text("<<")); //.textColor(0x40FF4000) + hlayout.addChild((new TextWidget()).text("Several").alignment(Align.Center)); + hlayout.addChild((new ImageWidget()).drawableId("exit").padding(Rect(5,5,5,5)).alignment(Align.Center)); + hlayout.addChild((new TextWidget()).text("items").alignment(Align.Center)); + hlayout.addChild((new ImageWidget()).drawableId("exit").padding(Rect(5,5,5,5)).alignment(Align.Center)); + hlayout.addChild((new TextWidget()).text("in horizontal layout")); + hlayout.addChild((new ImageWidget()).drawableId("exit").padding(Rect(5,5,5,5)).alignment(Align.Center)); + //hlayout.addChild((new Button()).text(">>")); //.textColor(0x40FF4000) + hlayout.backgroundColor = 0x8080C0; + layout.addChild(hlayout); - LinearLayout vlayout = new VerticalLayout(); - vlayout.addChild((new TextWidget()).text("VLayout line 1").textColor(0x40FF4000)); // - vlayout.addChild((new TextWidget()).text("VLayout line 2").textColor(0x40FFFF00)); - layout.addChild(vlayout); + LinearLayout vlayout = new VerticalLayout(); + vlayout.addChild((new TextWidget()).text("VLayout line 1").textColor(0x40FF4000)); // + vlayout.addChild((new TextWidget()).text("VLayout line 2").textColor(0x40FFFF00)); + layout.addChild(vlayout); - layout.addChild((new Button("BTN2")).textColor(0x000000FF).text("Button2")); - layout.addChild((new TextWidget()).textColor(0x40FF4000).text("Text widget")); - layout.addChild((new ImageWidget()).drawableId("exit").padding(Rect(5,5,5,5))); - layout.addChild((new TextWidget()).textColor(0xFF4000).text("Text widget2").padding(Rect(5,5,5,5)).margins(Rect(5,5,5,5)).backgroundColor(0xA0A0A0)); - layout.addChild((new Button("BTN3")).textColor(0x000000FF).text("Button3").layoutHeight(FILL_PARENT)); - layout.addChild((new TextWidget()).textColor(0x004000).text("Text widget3 with very long text")); + layout.addChild((new Button("BTN2")).textColor(0x000000FF).text("Button2")); + layout.addChild((new TextWidget()).textColor(0x40FF4000).text("Text widget")); + layout.addChild((new ImageWidget()).drawableId("exit").padding(Rect(5,5,5,5))); + layout.addChild((new TextWidget()).textColor(0xFF4000).text("Text widget2").padding(Rect(5,5,5,5)).margins(Rect(5,5,5,5)).backgroundColor(0xA0A0A0)); + layout.addChild((new Button("BTN3")).textColor(0x000000FF).text("Button3").layoutHeight(FILL_PARENT)); + layout.addChild((new TextWidget()).textColor(0x004000).text("Text widget3 with very long text")); - layout.childById("BTN1").onClickListener(delegate (Widget w) { Log.d("onClick ", w.id); return true; }); - layout.childById("BTN2").onClickListener(delegate (Widget w) { Log.d("onClick ", w.id); return true; }); - layout.childById("BTN3").onClickListener(delegate (Widget w) { Log.d("onClick ", w.id); return true; }); + layout.childById("BTN1").onClickListener(delegate (Widget w) { Log.d("onClick ", w.id); return true; }); + layout.childById("BTN2").onClickListener(delegate (Widget w) { Log.d("onClick ", w.id); return true; }); + layout.childById("BTN3").onClickListener(delegate (Widget w) { Log.d("onClick ", w.id); return true; }); - layout.layoutHeight(FILL_PARENT).layoutWidth(FILL_PARENT); + layout.layoutHeight(FILL_PARENT).layoutWidth(FILL_PARENT); - window.mainWidget = layout; + window.mainWidget = layout; + } else { + window.mainWidget = (new Button()).text("sample button"); + } window.show(); window.windowCaption = "New Window Caption"; diff --git a/src/dlangui/graphics/drawbuf.d b/src/dlangui/graphics/drawbuf.d index 9ac4fa0b..82f6790a 100644 --- a/src/dlangui/graphics/drawbuf.d +++ b/src/dlangui/graphics/drawbuf.d @@ -699,12 +699,18 @@ class SolidFillDrawable : Drawable { class ImageDrawable : Drawable { protected DrawBufRef _image; protected bool _tiled; + private int _instanceCount; this(ref DrawBufRef image, bool tiled = false, bool ninePatch = false) { _image = image; _tiled = tiled; if (ninePatch) _image.detectNinePatch(); + Log.d("Created ImageDrawable, count=", ++_instanceCount); } + ~this() { + _image.clear(); + Log.d("Destroyed ImageDrawable, count=", --_instanceCount); + } @property override int width() { if (_image.isNull) return 0; diff --git a/src/dlangui/graphics/ftfonts.d b/src/dlangui/graphics/ftfonts.d index 7170bf4b..54ebc461 100644 --- a/src/dlangui/graphics/ftfonts.d +++ b/src/dlangui/graphics/ftfonts.d @@ -475,7 +475,7 @@ class FreeTypeFontManager : FontManager { return best; } - private FontList _activeFonts; + //private FontList _activeFonts; private static FontRef _nullFontRef; @@ -490,13 +490,14 @@ class FreeTypeFontManager : FontManager { } } ~this() { - Log.d("FreeTypeFontManager ~this() active fonts: ", _activeFonts.length); - _activeFonts.clear(); + Log.d("FreeTypeFontManager ~this()"); + //_activeFonts.clear(); foreach(ref FontFileItem item; _fontFiles) { destroy(item); item = null; } _fontFiles.length = 0; + Log.d("Destroyed all fonts. Freeing library."); // uninit library if (_library) FT_Done_FreeType(_library); diff --git a/src/dlangui/graphics/images.d b/src/dlangui/graphics/images.d index 7c7f628b..ee0002b0 100644 --- a/src/dlangui/graphics/images.d +++ b/src/dlangui/graphics/images.d @@ -72,6 +72,11 @@ class ImageCache { } ~this() { Log.i("Destroying ImageCache"); + foreach (ref item; _map) { + destroy(item); + item = null; + } + _map.clear(); } } @@ -108,14 +113,17 @@ class DrawableCache { bool _error; bool _used; DrawableRef _drawable; + private int _instanceCount; this(string id, string filename, bool tiled) { _id = id; _filename = filename; _tiled = tiled; _error = filename is null; + Log.d("Created DrawableCacheItem, count=", ++_instanceCount); } ~this() { _drawable.clear(); + Log.d("Destroyed DrawableCacheItem, count=", --_instanceCount); } /// remove from memory, will cause reload on next access void compact() { @@ -213,6 +221,11 @@ class DrawableCache { } ~this() { Log.i("Destroying DrawableCache"); + foreach (ref item; _idToDrawableMap) { + destroy(item); + item = null; + } + _idToDrawableMap.clear(); } } diff --git a/src/dlangui/widgets/styles.d b/src/dlangui/widgets/styles.d index 81605437..972b70dd 100644 --- a/src/dlangui/widgets/styles.d +++ b/src/dlangui/widgets/styles.d @@ -472,6 +472,11 @@ class Theme : Style { ~this() { Log.d("Theme destructor"); + foreach(ref Style item; _byId) { + destroy(item); + item = null; + } + _byId.clear(); } /// create wrapper style which will have currentTheme.get(id) as parent instead of fixed parent - to modify some base style properties in widget @@ -520,6 +525,10 @@ class Theme : Style { return _byId[id]; return this; } + + void dumpStats() { + Log.d("Theme ", _id, ": children:", _children.length, ", substates:", _substates.length, ", mapsize:", _byId.length); + } } /// to access current theme @@ -542,6 +551,7 @@ Theme createDefaultTheme() { button.createState(State.Disabled, State.Disabled).backgroundImageId("btn_default_small_normal_disable"); button.createState(State.Pressed, State.Pressed).backgroundImageId("btn_default_small_pressed"); button.createState(State.Focused, State.Focused).backgroundImageId("btn_default_small_selected"); + res.dumpStats(); return res; }