简体   繁体   English

当使用new()运算符分配内存时,mudflap会抛出核心转储

[英]mudflap throws core dump when using new() operator to allocate memory

Here is my code snippet. 这是我的代码片段。

int main()
    {
    int *var = new int(6);
    cout<<"Hello\n";
    delete var;
    return 0;
}

when compiled with mudflap as 当用mudflap编译时

$export MUDFLAP_OPTIONS="-print-leaks -mode-check"
$g++ test.cpp -fmudflap -lmudflap
$./a.out
Segmentation fault (core dumped)

But when compiled without mudflap option it doesn't throw core dump. 但是在没有mudflap选项的情况下进行编译时,它不会抛出核心转储。 I'm new to mudflap. 我是mudflap的新手。 Kindly tell if I'm using mudflap in wrong way. 请告诉我是否以错误的方式使用mudflap。

FYI: 供参考:

$uname -a
Linux localhost.localdomain 2.6.18-308.4.1.el5 #1 SMP Wed Mar 28 01:54:56 EDT 2012 x86_64 x86_64 x86_64 GNU/Linux
$g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux-gnu/4.7.3/lto-wrapper
Target: x86_64-redhat-linux-gnu
Configured with: /root/rohit/gcc-4.7.3/configure --prefix=/usr/
Thread model: posix
gcc version 4.7.3 (GCC)

bt full of coredump 充满了coredump

Core was generated by `./a.out'. 核心由`./a.out'生成。

Program terminated with signal 11, Segmentation fault.
[New process 22176]
 #0  0x0000003ca5e075c8 in ?? () from /lib64/libgcc_s.so.1
(gdb) bt ful
 #0  0x0000003ca5e075c8 in ?? () from /lib64/libgcc_s.so.1
No symbol table info available.
 #1  0x0000003ca5e0882b in _Unwind_Backtrace () from /lib64/libgcc_s.so.1
No symbol table info available.
 #2  0x0000003c96ce5eb8 in backtrace () from /lib64/libc.so.6
No symbol table info available.
 #3  0x00002b4acf58b417 in __mf_backtrace (symbols=0x6a51db8, guess_pc=0x2b4acf58d351, guess_omit_levels=2)
    at /root/rohit/gcc-4.7.3/libmudflap/mf-runtime.c:1981
        pc_array = (void **) 0x6a51e00
        pc_array_size = 6
        remaining_size = <value optimized out>
        omitted_size = Unhandled dwarf expression opcode 0x9f
        i = <value optimized out>
 #4  0x0000000000000002 in ?? ()
No symbol table info available.
 #5  0x0000000000000004 in ?? ()
No symbol table info available.
 #6  0x0000000000000000 in ?? ()
No symbol table info available.

Your sample compiles As-Is on my compiler (Solaris on x86), but really, it is so simple that the platform shouldn't make a difference. 您的示例在我的编译器(x86上的Solaris)上编译As-Is,但实际上,它非常简单,平台不应该有所作为。 As written, your code should compile and run everywhere. 如上所述,您的代码应该在任何地方编译和运行。 It would appear to be some shortcoming of MudFlap. 这似乎是MudFlap的一些缺点。

I don't have access to MudFlap in my current environment but here's something which might enable you to avoid the problem altogether: 我目前的环境中无法访问MudFlap,但这里可以让您完全避免这个问题:

#include <iostream>
#include <boost/shared_ptr.hpp>
using namespace std;

int main() {
 boost::shared_ptr<int> var (new int(6));
 cout << "Hello #" << *var << endl;
 return 0;
}

I'm making a wild guess that "hiding" the dynamic allocation inside the smart-pointer initialization will trick MudFlap. 我疯狂地猜测,在智能指针初始化中“隐藏”动态分配会欺骗MudFlap。

I'll guess that your snippet is your simplification of a real-world problem. 我猜你的片段是你对现实世界问题的简化。 While I have no clue why even the simplified version wouldn't compile in your environment (it should), the above solution has the virtue that you don't need to worry about deleting the pointer, however large the real-world scope is. 虽然我不知道为什么即使简化版本也不能在您的环境中编译(它应该),上述解决方案的优点是您不必担心删除指针,无论现实范围多大。

Then again, maybe MudFlap is really smart and attempting to force you to use RAII. 然后,也许MudFlap真的很聪明,并试图强迫你使用RAII。 Perhaps it has some configuration setting somewhere to let you specify that you want to use and manage raw pointers. 也许它有一些配置设置允许您指定要使用和管理原始指针。 Whatever the case, you would be well-served to go with the smart-pointer approach, regardless. 无论如何,无论如何,你都可以顺利使用智能指针方法。

Try it and let us know if it helps. 尝试一下,让我们知道它是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM