编辑:为清楚起见,我将保持原样。问题似乎是
Kameleon
该类使用boost
,并且由于我自己的代码也使用它,因此可能存在冲突的版本,这就是问题所在。
原始问题:
很多信息,但我试图将其归结为有趣的部分。我正在编写一个应用程序,它使用类的实例Kameleon
(我自己没有编写)来执行各种任务。当我尝试使用new
关键字分配实例时,我遇到了麻烦。这是问题的缩小版本:
#include <ccmc/Kameleon.h>
int main() {
ccmc::Kameleon k;
ccmc::Kameleon *k2 = new ccmc::Kameleon(); // <-- crashes with this line
delete k2:
return 0;
}
旁注:注释掉new
分配k2
并仅运行ccmc::Kameleon k
作品,我可以使用该变量。但是,当我尝试时 main() 返回时,程序会出现段错误。析构函数什么都不做。
`Kameleon 构造函数执行以下操作:
// Kameleon.cpp
/*47*/ Kameleon::Kameleon() : model(NULL), // model is a non-const pointer
/*48*/ modelName("NA"), // modelName is a non-const std::string
/*49*/ missingValue(0.f) // missingValue is a non-const float
/*50*/ {}
我已经尝试了错误消息、gdb 和 valgrind 的问题,但似乎找不到源。这是运行程序给我的:
FurnaceApp: malloc.c:2451: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
Aborted (core dumped)
gdb 告诉我以下内容:
(gdb) bt
#0 0x00007ffff6b1c425 in __GI_raise (sig=<optimized out>) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
#1 0x00007ffff6b1fb8b in __GI_abort () at abort.c:91
#2 0x00007ffff6b6415d in __malloc_assert (assertion=<optimized out>, file=<optimized out>, line=<optimized out>, function=<optimized out>)
at malloc.c:300
#3 0x00007ffff6b67664 in sYSMALLOc (av=0x7ffff6e9e720, nb=48) at malloc.c:2448
#4 _int_malloc (av=0x7ffff6e9e720, bytes=27) at malloc.c:3892
#5 0x00007ffff6b68fb5 in __GI___libc_malloc (bytes=27) at malloc.c:2924
#6 0x00007ffff746cded in operator new(unsigned long) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#7 0x00007ffff7455a89 in std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) ()
from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#8 0x00007ffff7457495 in char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator<char> const&, std::forward_iterator_tag) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#9 0x00007ffff74575e3 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#10 0x00007ffff7757caf in ccmc::Kameleon::Kameleon (this=0x67d920) at Kameleon.cpp:49
#11 0x0000000000415516 in main ()
最后,valgrind 给了我很多输出,但这部分看起来最像前面的错误:
==11789== Invalid write of size 8
==11789== at 0x52ECC8D: ccmc::Kameleon::Kameleon() (buckets.hpp:128)
==11789== by 0x415515: main (in /home/vsand/OpenSpace/Furnace/FurnaceApp)
==11789== Address 0x6683a00 is 0 bytes after a block of size 464 alloc'd
==11789== at 0x4C2B1C7: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==11789== by 0x41550A: main (in /home/vsand/OpenSpace/Furnace/FurnaceApp)
==11789==
==11789== Invalid write of size 8
==11789== at 0x52ECC94: ccmc::Kameleon::Kameleon() (table.hpp:226)
==11789== by 0x415515: main (in /home/vsand/OpenSpace/Furnace/FurnaceApp)
==11789== Address 0x6683a28 is not stack'd, malloc'd or (recently) free'd
==11789==
==11789== Invalid write of size 8
==11789== at 0x52ECC9F: ccmc::Kameleon::Kameleon() (Kameleon.cpp:49)
==11789== by 0x415515: main (in /home/vsand/OpenSpace/Furnace/FurnaceApp)
==11789== Address 0x6683a30 is not stack'd, malloc'd or (recently) free'd
==11789==
到处看看,似乎这些错误通常在使用malloc
不当和超出分配的内存等写入时出现。类中有很多代码Kameleon
,但由于我自己没有编写它,所以我在尝试追踪它时遇到了问题。任何错误搜寻技巧将不胜感激!