开发者

error managing memory

开发者 https://www.devze.com 2023-01-22 16:49 出处:网络
I have a error in my code. I become on mac a incorrect checksum for freed object and under Linux a malloc error. Can somebody help me?

I have a error in my code. I become on mac a incorrect checksum for freed object and under Linux a malloc error. Can somebody help me?

/*
 * Copyright 2010, The PLDesktop Development Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */
#ifndef PLBUFFER_H
#define PLBUFFER_H

#include 
#include 
#include 
#include 
#include 

namespace PLSDK {

/**
  * Buffer for all parts of data.
  *
  * @ingroup tools_core
  */
template
class PLBuffer : public virtual PLObject {
public:
   /**
     * Constructor.
     */
   PLBuffer() : PLObject(), m_capacity(0), m_count(0), m_data(0) {}
   /**
     * Constructor.
     *
     * @param size The size of the buffer.
     */
   PLBuffer(pluint size);
   /**
     * Constructor.
     *
     * @param b Element to add to the buffer.
     */
   PLBuffer(bufferT b);
   /**
     * Constructor.
     *
     * @param b Data to set into the buffer.
     * @param len Length of the data.
     */
   PLBuffer(bufferT *b, pluint len);
   /**
     * Constructor.
     *
     * @param b Buffer to copy.
     */
   PLBuffer(PLBuffer *b);
   /**
     * Constructor.
     *
     * @param b Buffer to copy.
     */
   PLBuffer(const PLBuffer &b);
   /**
     * Destructor.
     */
   virtual ~PLBuffer();
   /**
     * Data from the buffer.
     *
     * @return Data from the buffer.
     */
   inline bufferT *data() const;
   /**
     * The size of the buffer.
     *
     * @return The size of the buffer.
     */
   inline pluint capacity() const;
   /**
     * How many parts are in the buffer.
     *
     * @return The number of parts that are in the buffer.
     */
   inline pluint count() const;
   /**
     * Is the buffer a empty buffer.
     *
     * @return True if the buffer is empty.
     */
   inline bool isEmpty() const;
   /**
     * Set the data as data from the buffer.
     * This delete all data that was set before.
     *
     * @param b Data to set.
     * @param len Length of the data to set.
     */
   inline void setBuffer(bufferT *b, pluint len);
   /**
     * Clear the buffer and delete all memory that was mapped for the buffer.
     */
   inline void clear();
   /**
     * Don't delete the mapped data but set the size to null. So we overright all
     * data.
     */
   inline void clean();
   /**
     * Set all data to null and than set the size to null. So no data was in the ram
     * and we don't remap data.
     */
   inline void secureClean();
   /**
     * Append one data at the end of the buffer.
     *
     * @param d Data to append;
     */
   inline void append(const bufferT d);
   /**
     * Append a array of data to the buffer.
     *
     * @param d Array of data.
     * @param len Length of the array of data.
     * @exception PLAppendEmptyElementException The length of the data are empty.
     */
   inline void append(const bufferT *d, pluint len) throw(PLAppendEmptyElementException);
   /**
     * Append a buffer of data to the buffer.
     *
     * @param d Array of data.
     * @exception PLAppendEmptyElementException The length of the data are empty.
     */
   inline void append(PLBuffer *d) throw(PLAppendEmptyElementException);
   /**
     * Append a buffer of data to the buffer.
     *
     * @param d Array of data.
     * @exception PLAppendEmptyElementException The length of the data are empty.
     */
   inline void append(const PLBuffer &d) throw(PLAppendEmptyElementException);
   /**
     * Cut data out of the buffer.
     *
     * @param s Start point to cut.
     * @param e End point to cut.
     * @return The data that we cut out of the buffer.
     * @exception PLEmptyElementException The returnd value was a empty value.
     */
   inline bufferT *cutData(pluint s, pluint e) throw(PLEmptyElementException);
   /**
     * Ends the data of the buffer with the data of another buffer?
     *
     * @param b Buffer to test.
     * @return True if the buffer ends with the data of the buffer b.
     */
   inline bool endsWith(PLBuffer *b);
   /**
     * Ends the data of the buffer with the data of another buffer?
     *
     * @param b Buffer to test.
     * @return True if the buffer ends with the data of the buffer b.
     */
   inline bool endsWith(const PLBuffer &b);
   /**
     * Get the data of a part from the buffer.
     *
     * @param s Startpoint of the part.
     * @param e Endpoint of the part.
     * @return The part of the buffer.
     */
   inline bufferT *data(pluint s, pluint e);
   /**
     * Return a buffer with a part of this buffer.
     *
     * @param s Start point in the buffer.
     * @param e End point in the buffer.
     * @return The buffer with the part of the buffer.
     */
   inline PLBuffer subPart(pluint s, pluint e);
   /**
     * Return the data at the point p.
     *
     * @param p Point for that you want the data.
     * @return The data at the position p.
     */
   inline bufferT dataAt(pluint p);
   /**
     * Return the first index of a component.
     *
     * @param c The component to search for.
     * @param s Start position to search for.
     * @return The first index of the component c.
     * @exception PLIndexOutOfRangeException The start position was out of range.
     * @exception PLElementNotFoundException The element was not found in the buffer.
     */
   inline int indexOf(bufferT c, pluint s = 0) throw(PLIndexOutOfRangeException, PLElementNotFoundException);
   /**
     * Return the first index of a buffer.
     *
     * @param c The buffer to search for.
     * @param s Start position to search for.
     * @return The first index of the component c.
     * @exception PLIndexOutOfRangeException The start position was out of range.
     * @exception PLElementNotFoundException The element was not found in the buffer.
     */
   inline int indexOf(PLBuffer c, pluint s = 0) throw(PLIndexOutOfRangeException, PLElementNotFoundException);
   /**
     * Insert a component at the position p.
     *
     * @param c The component to add.
     * @param p Position where to add the component.
     */
   inline void insertAt(bufferT c, pluint p);
   /**
     * Insert a component array at the position p.
     *
     * @param c The component array to add.
     * @param len The length of the component array.
     * @param p Position where to add the component.
     */
   inline void insertAt(bufferT *c, pluint len, pluint p);
   /**
     * Find the last position of c.
     *
     * @param c Component to search for.
     * @param s Start position to start for.
     * @return The last position of c.
     * @exception PLIndexOutOfRangeException The start position was out of range.
     * @exception PLElementNotFoundException The element was not found in the buffer.
     */
   inline int lastIndexOf(bufferT c, int s = -1) throw(PLIndexOutOfRangeException, PLElementNotFoundException);
   /**
     * Find the last position of c.
     *
     * @param c Component to search for.
     * @param s Start position to start for.
     * @return The last position of c.
     * @exception PLIndexOutOfRangeException The start position was out of range.
     * @exception PLElementNotFoundException The element was not found in the buffer.
     */
   inline int lastIndexOf(PLBuffer c, int s = -1) throw(PLIndexOutOfRangeException, PLElementNotFoundException);
   /**
     * Insert a component at the beginning of the buffer.
     *
     * @param c Component to add.
     */
   inline void prepend(bufferT c);
   /**
     * Insert a component at the beginning of the buffer.
     *
     * @param c Component to add.
     * @param len The length of the component.
     */
   inline void prepend(bufferT *c, pluint len);
   /**
     * Prepend a buffer of data to the buffer.
     *
     * @param d Array of data.
     */
   inline void prepend(PLBuffer *d);
   /**
     * Prepend a buffer of data to the buffer.
     *
     * @param d Array of data.
     */
   inline void prepend(PLBuffer d);
   /**
     * Remove a part of the buffer.
     *
     * @param s Startpoint for the delete part.
     * @param e Endpoint for the delete part.
     */
   inline void remove(pluint s, pluint e);
   /**
     * Array of components.
     *
     * @return The array of the components.
     */
   inline bufferT *array();
   /**
     * Write a component to the buffer.

* It overright the existing component. * * @param p Position where to add the component. * @param c Component that you want to set. */ inline void write(pluint p, bufferT c); /** * Write a component array to the buffer.

* It overright the existing component. * * @param p Position where to add the component. * @param c Component array that you want to set. * @param len Length of the component array. */ inline void write(pluint p, bufferT *c, pluint len); /** * Read the component from the position p. * * @param p Position of the component you want to read. * @return The component at the position p. */ inline bufferT read(pluint p); /** * Read the component from the position p. * * @param p Position of the component you want to read. * @param len The length of the component you want to read. * @return The component at the position p. */ inline bufferT *read(pluint p, pluint len); /** * Compare the buffer with another buffer data. * * @param data Buffer to compare to. * @return The compare result. */ inline PLCompareResult compareTo(PLBuffer data); /** * Contains the buffer a value. * * @param value The value to test for a match for. * @return True if the buffer contains the value. */ inline bool contains(bufferT value); /** * Contains the buffer another buffer data. * * @param data The data to test for a match for. * @param length The length of the data. * @return True if the buffer contains the buffer. */ inline bool contains(bufferT *data, pluint length); /** * Replace the part replace with the part with in this buffer. * * @param replace Part to search for. * @param with Part to replace with. */ inline void replaceAll(PLBuffer *replace, PLBuffer *with); /** * Operator for the direct access to the data. */ inline bufferT operator[](int pos); /** * Compare the buffer. */ inline bool operator == (PLBuffer data); /** * Compare the buffer. */ inline bool operator != (PLBuffer data); /** * Is the buffer smaler than another buffer. */ inline bool operator data); /** * Is the buffer lager than another buffer. */ inline bool operator > (PLBuffer data); /** * Copy the buffer. */ inline PLBuffer &operator =(const PLBuffer &buffer); protected: /** * Resize the buffer to the given size. * * @param c The size to resize to. */ void resize(pluint c) { if(m_data == 0) { m_data = new bufferT[c]; m_capacity = c; m_count = 0; return; } if(m_count + c > m_capacity) { bufferT *t = new bufferT[m_count + c]; if(m_data != 0) { for(pluint i = 0 ; i PLBuffer::PLBuffer(pluint size) : PLObject(), m_data(0), m_capacity(0), m_count(0) { if(size > 0) { m_data = new bufferT[size]; m_capacity = size; } } template PLBuffer::PLBuffer(bufferT b) : PLObject(), m_capacity(1), m_count(1) { m_data = new bufferT[1]; m_data[0] = b; } template PLBuffer::PLBuffer(bufferT *b, pluint len) : PLObject(), m_data(0), m_capacity(len), m_count(len) { if(len > 0) { m_data = new bufferT[len]; pluint i = 0; while(i PLBuffer::PLBuffer(PLBuffer *b) : PLObject(), m_data(), m_capacity(b->m_count), m_count(b->m_count) { if(b->m_count > 0) { m_data = new bufferT[b->m_count]; pluint i = 0; if(b->m_data != 0) { 开发者_如何学编程 while(i m_count) { m_data[i] = b->m_data[i]; i++; } } } } template PLBuffer::PLBuffer(const PLBuffer &b) : PLObject(), m_data(0), m_capacity(b.m_count), m_count(b.m_count) { if(b.m_count > 0) { m_data = new bufferT[b.m_count]; pluint i = 0; if(b.m_data != 0) { while(i PLBuffer::~PLBuffer() { if(m_data != 0) { delete m_data; m_data = 0; } } template bufferT *PLBuffer::data() const { return m_data; } template pluint PLBuffer::capacity() const { return m_capacity; } template pluint PLBuffer::count() const { return m_count; } template bool PLBuffer::isEmpty() const { return m_count == 0; } template void PLBuffer::setBuffer(bufferT *b, pluint len) { delete m_data; m_data = b; m_capacity = len; m_count = len; } template void PLBuffer::clear() { delete m_data; m_data = 0; m_capacity = 0; m_count = 0; } template void PLBuffer::clean() { m_count = 0; } template void PLBuffer::secureClean() { for(int i = 0 ; i void PLBuffer::append(const bufferT d) { resize(1); m_data[m_count] = d; m_count++; } template void PLBuffer::append(const bufferT *d, pluint len) throw(PLAppendEmptyElementException) { if(len void PLBuffer::append(PLBuffer *d) throw(PLAppendEmptyElementException) { if(d->count() m_data, d->m_count); } template void PLBuffer::append(const PLBuffer &d) throw(PLAppendEmptyElementException) { if(d.count() bufferT *PLBuffer::cutData(pluint s, pluint e) throw(PLEmptyElementException) { if(e - s == 0) { PL_EXCEPTION(PLEmptyElementException); } if(e >= m_count) { e = m_count - 1; } if(e > s) { plSwap(e, s); } bufferT *ret = data(s, e); remove(s, e); return ret; } template bool PLBuffer::endsWith(PLBuffer *b) { if(m_count - 1 - b->m_count m_count - 1; for(int i = m_count - 1 ; i >= m_count - 1 - b->m_count ; i--) { if(m_data[i] != b->m_data[j]) { return false; } j--; } return true; } template bool PLBuffer::endsWith(const PLBuffer &b) { return endsWith(&b); } template bufferT *PLBuffer::data(pluint s, pluint e) { if(e (e, s); } if(e - s == 0) { return 0; } if(e > m_count) { e = m_count; } bufferT *ret = new bufferT[e - s]; for(pluint i = 0 ; i PLBuffer PLBuffer::subPart(pluint s, pluint e) { return PLBuffer(data(s, e), e - s);; } template bufferT PLBuffer::dataAt(pluint p) { if(p int PLBuffer::indexOf(bufferT c, pluint s) throw(PLIndexOutOfRangeException, PLElementNotFoundException) { if(s >= m_count) { PL_EXCEPTION(PLIndexOutOfRangeException); } for(pluint i = s ; i int PLBuffer::indexOf(PLBuffer c, pluint s) throw(PLIndexOutOfRangeException, PLElementNotFoundException) { if(s + c.m_count > m_count) { PL_EXCEPTION(PLIndexOutOfRangeException); } for(pluint i = s ; i void PLBuffer::insertAt(bufferT c, pluint p) { resize(1); for(pluint i = m_count - 1 ; i >= p ; i--) { m_data[i + 1] = m_data[i]; } m_data[p] = c; m_count++; } template void PLBuffer::insertAt(bufferT *c, pluint len, pluint p) { resize(len); int i; for(i = m_count - 1; i >= p ; i--) { if(i int PLBuffer::lastIndexOf(bufferT c, int s) throw(PLIndexOutOfRangeException, PLElementNotFoundException) { if(s = m_count) { PL_EXCEPTION(PLIndexOutOfRangeException); } for(pluint i = s ; i >= 0 ; i--) { if(m_data[i] == c) { return i; } } PL_EXCEPTION(PLElementNotFoundException); } template int PLBuffer::lastIndexOf(PLBuffer c, int s) throw(PLIndexOutOfRangeException, PLElementNotFoundException) { if(s = m_count) { PL_EXCEPTION(PLIndexOutOfRangeException); } for(pluint i = s ; i > c.count() ; i--) { for(pluint j = 0 ; j > c.count() ; j++) { if(m_data[i + j] != c.m_data[j]) { break; } if(j == 0) { return i; } } } PL_EXCEPTION(PLElementNotFoundException); } template void PLBuffer::prepend(bufferT c) { insertAt(c, 0); } template void PLBuffer::prepend(bufferT *c, pluint len) { insertAt(c, len, 0); } template void PLBuffer::prepend(PLBuffer *d) { prepend(d->m_data, d->m_count); } template void PLBuffer::prepend(PLBuffer d) { prepend(d.m_data, d.m_count); } template void PLBuffer::remove(pluint s, pluint e) { pluint diff = e - s; if(diff == 0) { return; } if(diff (e, s); diff = e - s; } if(e > m_count) { e = m_count; } for(pluint i = s ; i bufferT *PLBuffer::array() { bufferT *b = new bufferT[m_count]; for(pluint i = 0 ; i void PLBuffer::write(pluint p, bufferT c) { if(p >= m_count) { resize(p - m_count); } m_data[p] = c; } template void PLBuffer::write(pluint p, bufferT *c, pluint len) { if(p + len >= m_count) { resize(p + len - m_count); } for(pluint i = 0 ; i bufferT PLBuffer::read(pluint p) { return dataAt(p); } template bufferT *PLBuffer::read(pluint p, pluint len) { return data(p, len); } template PLCompareResult PLBuffer::compareTo(PLBuffer data) { if(m_count == data.m_count) { for(pluint i = 0 ; i data.m_data[i]) { return PLCompareResultBigger; } } return PLCompareResultSame; } return PLCompareResultSmaller; } template void PLBuffer::replaceAll(PLBuffer *replace, PLBuffer *with) { for(int i = 0 ; i m_data[0]) { for(int j = 0 ; j m_count ; j++) { if(i + j > m_count) { return; } if(j == replace->m_count - 1 && m_data[i + j] != replace->m_data[j]) { remove(i, i + replace->m_count - 1); insertAt(with->m_data, with->m_count - 1, i); } else if(m_data[i + j] != replace->m_data[j]) { break; } } } } } template bool PLBuffer::contains(bufferT value) { for(int i = 0 ; i bool PLBuffer::contains(bufferT *data, pluint length) { int j; for(int i = 0 ; i bufferT PLBuffer::operator[](int pos) { return m_data[pos]; } template bool PLBuffer::operator == (PLBuffer data) { return compareTo(data) == PLCompareResultSame; } template bool PLBuffer::operator != (PLBuffer data) { return compareTo(data) != PLCompareResultSame; } template bool PLBuffer::operator data) { return compareTo(data) == PLCompareResultSmaller; } template bool PLBuffer::operator > (PLBuffer data) { return compareTo(data) == PLCompareResultBigger; } template PLBuffer &PLBuffer::operator =(const PLBuffer &b) { if(m_data == 0) { delete m_data; } m_data = new bufferT[b.m_count]; m_capacity = b.m_count; m_count = b.m_count; pluint i = 0; if(b.m_data != 0) { while(i

Can somebody help me? All code you can find under http://code.google.com/p/pldesktop/


Run your code under valgrind on Linux - that should take you straight to the bug.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号