我有一个用C++编写的代码,它可以在Visual Studio上正常工作,但是当我将其传输到Linux(QT Designer)时,出现如下所示的运行时错误:
*** glibc detected *** /home/hfarazi/Test/QT/Test-build-desktop/Test: malloc(): memory corruption: 0x082c2c80 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x6ff22)[0x42cf22]
/lib/i386-linux-gnu/libc.so.6(+0x718be)[0x42e8be]
/lib/i386-linux-gnu/libc.so.6(__libc_malloc+0x68)[0x4307f8]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_Znwj+0x27)[0x2c89d7]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_Znaj+0x1b)[0x2c8afb]
/home/hfarazi/Test/QT/Test-build-desktop/Test[0x804abdc]
/home/hfarazi/Test/QT/Test-build-desktop/Test[0x8062860]
/home/hfarazi/Test/QT/Test-build-desktop/Test[0x80644b5]
/home/hfarazi/Test/QT/Test-build-desktop/Test[0x806e449]
/home/hfarazi/Test/QT/Test-build-desktop/Test[0x80604d5]
/home/hfarazi/Test/QT/Test-build-desktop/Test[0x8049205]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0x3d6113]
/home/hfarazi/Test/QT/Test-build-desktop/Test[0x8049351]
======= Memory map: ========
00219000-002f7000 r-xp 00000000 07:00 527926 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
002f7000-002f8000 ---p 000de000 07:00 527926 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
002f8000-002fc000 r--p 000de000 07:00 527926 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
002fc000-002fd000 rw-p 000e2000 07:00 527926 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
002fd000-00304000 rw-p 00000000 00:00 0
00339000-00357000 r-xp 00000000 07:00 266017 /lib/i386-linux-gnu/ld-2.13.so
00357000-00358000 r--p 0001d000 07:00 266017 /lib/i386-linux-gnu/ld-2.13.so
00358000-00359000 rw-p 0001e000 07:00 266017 /lib/i386-linux-gnu/ld-2.13.so
003a2000-003b9000 r-xp 00000000 07:00 273114 /lib/i386-linux-gnu/libpthread-2.13.so
003b9000-003ba000 r--p 00016000 07:00 273114 /lib/i386-linux-gnu/libpthread-2.13.so
003ba000-003bb000 rw-p 00017000 07:00 273114 /lib/i386-linux-gnu/libpthread-2.13.so
003bb000-003bd000 rw-p 00000000 00:00 0
003bd000-00535000 r-xp 00000000 07:00 273100 /lib/i386-linux-gnu/libc-2.13.so
00535000-00537000 r--p 00178000 07:00 273100 /lib/i386-linux-gnu/libc-2.13.so
00537000-00538000 rw-p 0017a000 07:00 273100 /lib/i386-linux-gnu/libc-2.13.so
00538000-0053b000 rw-p 00000000 00:00 0
00777000-0079f000 r-xp 00000000 07:00 273104 /lib/i386-linux-gnu/libm-2.13.so
0079f000-007a0000 r--p 00028000 07:00 273104 /lib/i386-linux-gnu/libm-2.13.so
007a0000-007a1000 rw-p 00029000 07:00 273104 /lib/i386-linux-gnu/libm-2.13.so
00bf7000-00bf8000 r-xp 00000000 00:00 0 [vdso]
00df3000-00e0f000 r-xp 00000000 07:00 262077 /lib/i386-linux-gnu/libgcc_s.so.1
00e0f000-00e10000 r--p 0001b000 07:00 262077 /lib/i386-linux-gnu/libgcc_s.so.1
00e10000-00e11000 rw-p 0001c000 07:00 262077 /lib/i386-linux-gnu/libgcc_s.so.1
08048000-08075000 r-xp 00000000 07:00 401931 /home/hfarazi/Test/QT/Test-build-desktop/Test
08075000-08076000 r--p 0002c000 07:00 401931 /home/hfarazi/Test/QT/Test-build-desktop/Test
08076000-08077000 rw-p 0002d000 07:00 401931 /home/hfarazi/Test/QT/Test-build-desktop/Test
082ba000-082db000 rw-p 00000000 00:00 0 [heap]
b7600000-b7621000 rw-p 00000000 00:00 0
b7621000-b7700000 ---p 00000000 00:00 0
b77f4000-b77f8000 rw-p 00000000 00:00 0
b780a000-b780d000 rw-p 00000000 00:00 0
bff44000-bff65000 rw-p 00000000 00:00 0 [stack]
您能帮我发现错误吗?由于这是一个公司项目,因此我无法轻松发送代码。
提前谢谢。
认为问题出在这个类上,是个主意吗?
#define NOT_VALID NULL
#define STACK_INITIAL_ALLOC 10
#define STACK_CHUNK_ALLOC 10
template<class T>
class stack
{
public:
stack()
: value(0), length(0), allocated(0)
{ }
stack(const T &value)
: value(0), length(0), allocated(0)
{
push(value);
}
~stack()
{
if (value)
delete [] value;
}
void push(const T &_value)
{
if (length == allocated)
allocate(); // Allocate more memory
value[length++] = _value;
}
T &pop()
{
if (length > 0)
{
//shrink();
length--;
return value[length];
}
}
T &peak()
{
if (length > 0)
return value[length - 1];
//else
// throw runtime_error("stack empty");
}
// Return the number of length in the stack
size_t count() const
{
return length;
}
size_t get_allocated() const
{
return allocated;
}
private:
T *value; // The actual stack
size_t length; // Number of length in stack
size_t allocated; // Allocated length in stack
void copy(T *from, T *to)
{
for (size_t i = 0; i < length; i++)
*to++ = *from++;
}
void allocate()
{
if (value == 0)
{
allocated = STACK_INITIAL_ALLOC;
value = new T[allocated];
}
else
{
// We need to allocate more memory
size_t new_allocated = allocated + STACK_CHUNK_ALLOC;
T *new_value = new T[new_allocated];
// Copy from old stack to new stack
copy(value, new_value);
// Delete the old value
delete [] value;
allocated = new_allocated;
value = new_value;
}
}
// Shrink the stack, if lots of it is unused
void shrink()
{
// The limit which the number of length must be lower than
size_t shrink_limit = allocated - STACK_CHUNK_ALLOC * 2;
// Do not shrink if we will get lower than the initial allocation (shrink_limit > STACK_INITIAL_ALLOC)
if (shrink_limit > STACK_INITIAL_ALLOC && length < shrink_limit)
{
// We can shrink the allocated memory a little
size_t new_allocated = allocated - STACK_CHUNK_ALLOC;
T *new_value = new T[new_allocated];
copy(value, new_value);
delete [] value;
value = new_value;
allocated = new_allocated;
}
}
};
最佳答案
如果您不处理线程,那么如果您以某种方式读取/写入数组边界,则很可能会发生这种问题。
由于这种操作的结果是不确定的,因此该问题在某些特定的主机/编译器中可能看不到,并且仅当您将代码传输到另一台主机时才可能出现。
有时,如果您的程序很大,实际上很难弄清这类问题。
除了使用调试器之外,还有一个建议是消除部分代码(如果可能的话),并尝试缩小问题出处。
关于c++ - 内存泄漏C++,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10203032/