From 1bdf1ce3fa7fafa7360795bd3453161fb87bb721 Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Wed, 10 Dec 2014 15:17:00 +0300 Subject: [PATCH] fix memory leak - issue #19 --- src/dlangui/core/collections.d | 2 +- src/dlangui/widgets/editors.d | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/dlangui/core/collections.d b/src/dlangui/core/collections.d index ee0cc485..5a6e7392 100644 --- a/src/dlangui/core/collections.d +++ b/src/dlangui/core/collections.d @@ -82,7 +82,7 @@ struct Collection(T, bool ownItems = false) { void add(T item, size_t index = size_t.max) { if (index > _len) index = _len; - if (_items.length >= _len) { + if (_items.length <= _len) { if (_items.length < 4) _items.length = 4; else diff --git a/src/dlangui/widgets/editors.d b/src/dlangui/widgets/editors.d index 7970bcd8..c00a766a 100644 --- a/src/dlangui/widgets/editors.d +++ b/src/dlangui/widgets/editors.d @@ -292,12 +292,15 @@ class EditOperation { _action = action; _range = range; _content.length = 1; - _content[0] = text; + _content[0] = text.dup; } this(EditAction action, TextRange range, dstring[] text) { _action = action; _range = range; - _content = text; + _content.length = text.length; + for(int i = 0; i < text.length; i++) + _content[i] = text[i].dup; + //_content = text; } /// try to merge two operations (simple entering of characters in the same line), return true if succeded bool merge(EditOperation op) { @@ -318,12 +321,12 @@ class EditOperation { _range.start.pos--; _newRange.start.pos--; _newRange.end.pos--; - _oldContent[0] = (op._oldContent[0] ~ _oldContent[0]).dup; + _oldContent[0] = (op._oldContent[0].dup ~ _oldContent[0].dup).dup; return true; } else if (_newRange.end.pos == op._range.start.pos) { // removed char after _range.end.pos++; - _oldContent[0] = (_oldContent[0] ~ op._oldContent[0]).dup; + _oldContent[0] = (_oldContent[0].dup ~ op._oldContent[0].dup).dup; return true; } } @@ -354,7 +357,11 @@ class UndoBuffer { return; // merged - no need to add new operation } } + long ts = currentTimeMillis(); _undoList.pushBack(op); + long duration = currentTimeMillis() - ts; + if (duration > 3) + Log.w("Too long saveForUndo: ", duration, " ms"); } /// returns operation to be undone (put it to redo), null if no undo ops available