使用“new”关键字时,sYSMALLOc断言失败

时间:2013-04-22 20:43:37

标签: c++ gdb new-operator valgrind

  

编辑:为了清楚起见,我将保留问题原样。该   问题似乎是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;
 }

SIDE注意:注释掉new的{​​{1}}分配并且仅运行k2,我可以使用该变量。但是,当我尝试使用main()时,程序会出现段错误。析构函数什么都不做。

`Kameleon构造函数执行以下操作:

ccmc::Kameleon k

我已经尝试了我的错误消息,gdb和valgrind的问题,但似乎无法找到源。这就是程序运行的原因:

// 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告诉我以下内容:

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)

最后,valgrind给了我很多输出,但这部分看起来与以前的错误最相似:

(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 ()

在这里和那里四处看看似乎这些错误通常在使用==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类中有很多代码,但是因为我做了不写自己我试图追踪它有问题。任何错误狩猎技巧将非常感激!

2 个答案:

答案 0 :(得分:1)

你可以尝试:

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

modelName const是否有机会?

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

#include "Kameleon.h" // or whatever

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

Kameleon是派生类吗?它会使new运算符超载吗?

答案 1 :(得分: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()的代码写入字节[this+464, this+472)

最可能的原因:

  • 您已在buckets.hpp
  • 中更改了班级的定义
  • 您尚未重新编译使用它的所有代码,因此违反了one definition rule

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

  

buckets.hpp来自boost标题

另一种可能性是您正在链接一个针对不同版本的Boost编译的库。 无法工作, 使用完全相同版本的Boost。

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