0

编辑:为清楚起见,我将保持原样。问题似乎是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,但由于我自己没有编写它,所以我在尝试追踪它时遇到了问题。任何错误搜寻技巧将不胜感激!

4

2 回答 2

1

你能试一下吗:

Kameleon::Kameleon(): model(NULL), modelName("N/A") {
    // ...
}

modelNameconst 吗?

您还可以删除构造函数中的所有 unesseray 代码CDFReader以隔离错误吗?甚至更好:

#include "Kameleon.h" // or whatever

int main(){
    ccmc::Kameleon k;
    ccmc::Kameleon *k2 = new ccmc::Kameleon();
    delete k2;
}

Kameleon派生类吗?它会使new操作员过载吗?

于 2013-04-22T20:47:26.830 回答
1
==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)

这意味着:

  • 您分配实例的代码ccmc::Kameleon认为sizeof(ccmc::Kameleon)是 464,而
  • 您的代码实际上实现了ccmc::Kameleon::Kameleon()对 bytes 的写入[this+464, this+472)

最可能的原因:

  • 您已更改 中的类的定义buckets.hpp,并且
  • 您没有重新编译所有使用它的代码,因此违反了单一定义规则

必须重建所有使用 的代码Kameleon,一旦这样做,您的问题就会消失。

buckets.hpp 来自 boost 标头

另一种可能性是您正在链接一个针对不同版本的 Boost 编译的库。那行不通,您必须使用完全相同版本的Boost。

最后一种可能性是 Boost 是用一组不一致的-DXX标志编译的,导致Kameleon类的不同定义(并且再次违反了一个定义规则)。

于 2013-04-23T03:58:58.883 回答