fix memory leak; cosmetics

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@2396 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
André Pönitz 2001-08-01 09:18:21 +00:00
parent 605c3cbb36
commit e0aa02ee9f
5 changed files with 30 additions and 36 deletions

View File

@ -19,26 +19,31 @@ MathArray::MathArray()
MathArray::~MathArray() MathArray::~MathArray()
{ {
for (int pos = 0; pos < size(); next(pos)) erase();
if (isInset(pos))
delete nextInset(pos);
} }
MathArray::MathArray(MathArray const & array) MathArray::MathArray(MathArray const & array)
: bf_(array.bf_) : bf_(array.bf_)
{ {
for (int pos = 0; pos < size(); next(pos)) deep_copy(0, size());
if (isInset(pos))
replace(pos, nextInset(pos)->clone());
} }
MathArray::MathArray(MathArray const & array, int from, int to) MathArray::MathArray(MathArray const & array, int from, int to)
: bf_(array.bf_.begin() + from, array.bf_.begin() + to) : bf_(array.bf_.begin() + from, array.bf_.begin() + to)
{ {
for (int pos = 0; pos < size(); next(pos)) deep_copy(0, size());
if (isInset(pos)) }
replace(pos, nextInset(pos)->clone());
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; return p;
} }
MathInset * MathArray::prevInset(int pos) const MathInset * MathArray::prevInset(int pos) const
{ {
if (!pos) if (!prev(pos))
return 0; return 0;
prev(pos);
return nextInset(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) 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) void MathArray::insert(int pos, MathArray const & array)
{ {
bf_.insert(bf_.begin() + pos, array.bf_.begin(), array.bf_.end()); bf_.insert(bf_.begin() + pos, array.bf_.begin(), array.bf_.end());
for (int p = pos; p < pos + array.size(); next(p)) deep_copy(pos, pos + array.size());
if (isInset(p))
replace(p, nextInset(p)->clone());
} }
@ -200,10 +198,7 @@ void MathArray::push_back(MathArray const & array)
void MathArray::clear() void MathArray::clear()
{ {
for (int pos = 0; pos < size(); next(pos)) erase();
if (isInset(pos))
delete nextInset(pos);
bf_.clear();
} }
@ -241,6 +236,9 @@ void MathArray::erase(int pos)
void MathArray::erase(int pos1, int pos2) 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); bf_.erase(bf_.begin() + pos1, bf_.begin() + pos2);
} }
@ -255,13 +253,7 @@ bool MathArray::isInset(int pos) const
MathInset * MathArray::back_inset() const MathInset * MathArray::back_inset() const
{ {
if (!empty()) { return prevInset(size());
int pos = size();
prev(pos);
if (isInset(pos))
return nextInset(pos);
}
return 0;
} }
@ -374,6 +366,6 @@ void MathArray::pop_back()
{ {
int pos = size(); int pos = size();
prev(pos); prev(pos);
erase(pos); erase(pos, size());
} }

View File

@ -76,8 +76,6 @@ public:
/// ///
void erase(); void erase();
/// ///
void replace(int pos, MathInset * inset);
///
bool prev(int & pos) const; bool prev(int & pos) const;
/// ///
bool next(int & pos) const; bool next(int & pos) const;
@ -133,6 +131,8 @@ private:
/// ///
int item_size(int pos) const; int item_size(int pos) const;
///
void deep_copy(int pos1, int pos2);
/// Buffer /// Buffer
buffer_type bf_; buffer_type bf_;
}; };

View File

@ -135,7 +135,7 @@ InsetFormulaBase::InsetFormulaBase(MathInset * par)
InsetFormulaBase::InsetFormulaBase(InsetFormulaBase const & f) InsetFormulaBase::InsetFormulaBase(InsetFormulaBase const & f)
: UpdatableInset(f), par_(static_cast<MathInset *>(f.par_->clone())) : UpdatableInset(f), par_(f.par_->clone())
{} {}

View File

@ -121,9 +121,11 @@ public:
protected: protected:
/// ///
virtual void updateLocal(BufferView * bv, bool mark_dirty); virtual void updateLocal(BufferView * bv, bool mark_dirty);
/// ///
MathInset * par_; MathInset * par_;
private:
/// unimplemented
void operator=(const InsetFormulaBase &);
}; };
// We don't really mess want around with mathed stuff outside mathed. // We don't really mess want around with mathed stuff outside mathed.

View File

@ -29,7 +29,7 @@ void MathXArray::metrics(MathStyles st)
ascent_ = 0; ascent_ = 0;
descent_ = 0; descent_ = 0;
width_ = 0; width_ = 0;
style_ = st; style_ = st;
for (int pos = 0; pos < data_.size(); data_.next(pos)) { for (int pos = 0; pos < data_.size(); data_.next(pos)) {
int asc; int asc;