全局变量和类成员之间的gcc内存重叠

时间:2016-10-21 14:56:51

标签: gcc memory-leaks g++

我终于能够添加代码部分来解释我的问题......

我最近升级了我的开发PC,并将gcc从4.1升级到4.8。 编译相同的源我现在遇到var重叠问题。 我不能发布整个cource因为它很大但只是为了理解我在main()之前定义了一些变量,这些变量被定义为外部类的extern以便可以访问。 有些人的全局变量意外地改变它们的值并用gdb调试它我可以看到这个变量与一个类实例的一个成员共享相同的内存地址。

这是main.cpp上变量的定义变量loopcounter是我真正需要的唯一变量,我用未使用的变量包围它,以便更好地理解这种重叠。

Hash::check($value, $hashedValue)

我为gdb

设置了这个变量写访问的监视程序

watch loopcounter

当程序执行停止时,我可以看到我们在类Timer

的实例中

Hardware watchpoint 1: loopcounter

Old value = 541
New value = 66077


Timer::signal (this=0xc235b0 (loopcounteryyy), s=true) at chrono.cpp:229



这是chrono.cpp的一部分

uint64_t loopcounterx = 0;
uint64_t loopcounterxx = 0;
uint64_t loopcounterxxx = 0;
uint64_t loopcounterxxxx = 0;
uint64_t loopcounterxxxxx = 0;
uint64_t loopcounterxxxxxx = 0;
uint64_t loopcounter = 0;
uint64_t loopcountery = 0;
uint64_t loopcounteryy = 0;
uint64_t loopcounteryyy = 0;
uint64_t loopcounteryyyy = 0;
uint64_t loopcounteryyyyy = 0;
uint64_t loopcounteryyyyyy = 0;
uint64_t loopcounteryyyyyyy = 0;

.
.
.

int main(int argc, char *argv[]) {

看门狗停止执行包含

的chrono.cpp第229行
bool Timer::signal (bool s) {
    if ((s != in_signal_old) && !r_pulse) {
        in_signal = s;
        if (s) in_signal_fp=true;
        else in_signal_rp=true;
        in_signal_old = s;
    }
}

在停止时,内存违规已经发生,所以我认为我们必须考虑之前的那条线,那就是

if (s) in_signal_fp=true;

其中代码正在访问其类的成员,并且看起来该成员的写入似乎会损坏loopcounter变量的内容。 事实上,寻找指针我得到了这个



(gdb) p &loopcounter
$1 = (uint64_t *) 0xc235c8 (loopcounter) 

(gdb) p &in_signal
$2 = (uint64_t *) 0xc235ca (loopcounter + 2)

(gdb) p &in_signal_fp
$5 = (bool *) 0xc235cc (loopcounter+4)

(gdb) p &active_f
$7 = (bool *) 0xc235c8 (loopcounter)


active_f是chrono类的另一个成员,它与loopcounter变量完全重叠。

这是chrono.h头文件中包含的Timer类

in_signal = s;

为什么编译器/链接器或我不知道还有谁在同一个内存区域分配两个不同的对象?

我还使用了valgrind和gcc-4.8中引入的class Timer { private: Timer *me; Timer *prev; Timer *next; bool active_f; bool running_f; bool in_signal; bool in_signal_old; bool in_signal_fp; bool in_signal_rp; bool out_signal; bool out_signal_old; bool out_signal_fp; bool out_signal_rp; bool f_pulse; bool r_pulse; uint64_t start; uint64_t end; uint64_t lenght; timer_type type; public: int init (int lenght); bool out (void); bool active () { return active_f; } bool fp () { return f_pulse; } bool rp () { return r_pulse; } bool running () { return running_f; } void set_next (Timer *n) { next = n; } void set_prev (Timer *p) { prev = p; } void set_end (RTIME x) { end = x; } void set_active (bool x) { active_f = x; } void set_running (bool x) { running_f = x; } void set_lenght (RTIME x) { lenght = x; } void set_start (RTIME x) { start = x; } void set_type (timer_type x) { type = x; } void set_timer (RTIME x, timer_type y);// { lenght = x; type = y; } bool signal (bool); void set_fp (bool x) { f_pulse = x; } void set_rp (bool x) { r_pulse = x; } Timer * get_next () { return next; } Timer * get_prev () { return prev; } RTIME get_end () { return end; } RTIME get_start () { return start; } RTIME get_lenght (void) { return lenght; } void append (Timer *newt); Timer(); }; 选项,但我无法获得更多有用的信息或更多的工作地点。

我可以通过哪种其他方式调查此行为?

0 个答案:

没有答案