From e0aa02ee9f638810aa7508800fa6e683a8988891 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20P=C3=B6nitz?= Date: Wed, 1 Aug 2001 09:18:21 +0000 Subject: [PATCH] fix memory leak; cosmetics git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@2396 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/mathed/array.C | 54 +++++++++++++++++----------------------- src/mathed/array.h | 4 +-- src/mathed/formulabase.C | 2 +- src/mathed/formulabase.h | 4 ++- src/mathed/xarray.C | 2 +- 5 files changed, 30 insertions(+), 36 deletions(-) diff --git a/src/mathed/array.C b/src/mathed/array.C index 264002ea0a..47e6d32ccb 100644 --- a/src/mathed/array.C +++ b/src/mathed/array.C @@ -19,26 +19,31 @@ MathArray::MathArray() MathArray::~MathArray() { - for (int pos = 0; pos < size(); next(pos)) - if (isInset(pos)) - delete nextInset(pos); + erase(); } MathArray::MathArray(MathArray const & array) : bf_(array.bf_) { - for (int pos = 0; pos < size(); next(pos)) - if (isInset(pos)) - replace(pos, nextInset(pos)->clone()); + deep_copy(0, size()); } + MathArray::MathArray(MathArray const & array, int from, int to) : bf_(array.bf_.begin() + from, array.bf_.begin() + to) { - for (int pos = 0; pos < size(); next(pos)) - if (isInset(pos)) - replace(pos, nextInset(pos)->clone()); + deep_copy(0, size()); +} + + +void MathArray::deep_copy(int pos1, int pos2) +{ + for (int pos = pos1; pos < pos2; next(pos)) + if (isInset(pos)) { + MathInset * p = nextInset(pos)->clone(); + memcpy(&bf_[pos + 1], &p, sizeof(p)); + } } @@ -105,11 +110,11 @@ MathInset * MathArray::nextInset(int pos) const return p; } + MathInset * MathArray::prevInset(int pos) const { - if (!pos) + if (!prev(pos)) return 0; - prev(pos); return nextInset(pos); } @@ -151,11 +156,6 @@ void MathArray::setCode(int pos, MathTextCodes t) } -void MathArray::replace(int pos, MathInset * p) -{ - memcpy(&bf_[pos + 1], &p, sizeof(p)); -} - void MathArray::insert(int pos, MathInset * p) { @@ -174,9 +174,7 @@ void MathArray::insert(int pos, unsigned char b, MathTextCodes t) void MathArray::insert(int pos, MathArray const & array) { bf_.insert(bf_.begin() + pos, array.bf_.begin(), array.bf_.end()); - for (int p = pos; p < pos + array.size(); next(p)) - if (isInset(p)) - replace(p, nextInset(p)->clone()); + deep_copy(pos, pos + array.size()); } @@ -200,10 +198,7 @@ void MathArray::push_back(MathArray const & array) void MathArray::clear() { - for (int pos = 0; pos < size(); next(pos)) - if (isInset(pos)) - delete nextInset(pos); - bf_.clear(); + erase(); } @@ -241,6 +236,9 @@ void MathArray::erase(int pos) void MathArray::erase(int pos1, int pos2) { + for (int pos = pos1; pos < pos2; next(pos)) + if (isInset(pos)) + delete nextInset(pos); bf_.erase(bf_.begin() + pos1, bf_.begin() + pos2); } @@ -255,13 +253,7 @@ bool MathArray::isInset(int pos) const MathInset * MathArray::back_inset() const { - if (!empty()) { - int pos = size(); - prev(pos); - if (isInset(pos)) - return nextInset(pos); - } - return 0; + return prevInset(size()); } @@ -374,6 +366,6 @@ void MathArray::pop_back() { int pos = size(); prev(pos); - erase(pos); + erase(pos, size()); } diff --git a/src/mathed/array.h b/src/mathed/array.h index 6ff72c6a3c..f9c7e9bc30 100644 --- a/src/mathed/array.h +++ b/src/mathed/array.h @@ -76,8 +76,6 @@ public: /// void erase(); /// - void replace(int pos, MathInset * inset); - /// bool prev(int & pos) const; /// bool next(int & pos) const; @@ -133,6 +131,8 @@ private: /// int item_size(int pos) const; + /// + void deep_copy(int pos1, int pos2); /// Buffer buffer_type bf_; }; diff --git a/src/mathed/formulabase.C b/src/mathed/formulabase.C index 166b9d82f6..bed687f8a3 100644 --- a/src/mathed/formulabase.C +++ b/src/mathed/formulabase.C @@ -135,7 +135,7 @@ InsetFormulaBase::InsetFormulaBase(MathInset * par) InsetFormulaBase::InsetFormulaBase(InsetFormulaBase const & f) - : UpdatableInset(f), par_(static_cast(f.par_->clone())) + : UpdatableInset(f), par_(f.par_->clone()) {} diff --git a/src/mathed/formulabase.h b/src/mathed/formulabase.h index 73313dbc9b..1423136d35 100644 --- a/src/mathed/formulabase.h +++ b/src/mathed/formulabase.h @@ -121,9 +121,11 @@ public: protected: /// virtual void updateLocal(BufferView * bv, bool mark_dirty); - /// MathInset * par_; +private: + /// unimplemented + void operator=(const InsetFormulaBase &); }; // We don't really mess want around with mathed stuff outside mathed. diff --git a/src/mathed/xarray.C b/src/mathed/xarray.C index 636ef7d8a4..884a03b5ad 100644 --- a/src/mathed/xarray.C +++ b/src/mathed/xarray.C @@ -29,7 +29,7 @@ void MathXArray::metrics(MathStyles st) ascent_ = 0; descent_ = 0; width_ = 0; - style_ = st; + style_ = st; for (int pos = 0; pos < data_.size(); data_.next(pos)) { int asc;