使用SIGALRM和计时器时内存损坏

时间:2021-05-28 20:50:07

Ok not really sure if it is memory corruption or just a bug in my code, but here is the description of the problem: Single threaded program registers a SIGALRM handler:

好吧不确定它是内存损坏还是我的代码中的错误,但这里是问题的描述:单线程程序注册一个SIGALRM处理程序:

struct sigaction action;
sigset_t blocked;

sigfillset(&blocked);
sigdelset(&blocked, SIGKILL);
sigdelset(&blocked, SIGSTOP);

action.sa_handler = &handler_function;
action.sa_flags   = SA_RESTART;
action.sa_mask    = blocked;

// We are not interested in the previous action:
if (sigaction(SIGALRM, &action, NULL) == -1)
   ....

handler_function is a simple function which calls a function of an object:

handler_function是一个调用对象函数的简单函数:

void handler_function()
{
     some_object->tick();
}

tick()
{
    ++variable;
}

Variable is of type long. my program has only one instance of some_object and only signal handler calls function tick and only tick function modifies variable. Other functions only read value of variable. Forgot to mention that I create a timer:

变量类型很长。我的程序只有一个some_object实例,只有信号处理程序调用函数tick,只有tick函数修改变量。其他函数只读取变量的值。忘记提到我创建了一个计时器:

struct timeval interval;
interval.tv_sec   = 0;
interval.tv_usec  = (100000);

struct itimerval timer;
timer.it_interval = interval;
timer.it_value    = interval;
if (setitimer(ITIMER_REAL, &timer, NULL) == -1)
...

so tick function is triggered 10 times per second. Now the problem is that intermittently i get other data members of object some_object corrupted, they are POD types(long), i.e. all of a sudden they contain random values. I've tried to isolate a problem in a small program, but to no avail. Any advise would be appreciated. thanks

所以每秒触发滴答功能10次。现在的问题是间歇性地让对象some_object的其他数据成员损坏,它们是POD类型(长),即它们突然包含随机值。我试图在一个小程序中隔离一个问题,但无济于事。任何建议将不胜感激。谢谢

1 个解决方案

#1


0  

There's a buffer overflow in your socket reading code. Fix that and the problem will go away.

套接字读取代码中存在缓冲区溢出。解决这个问题,问题就会消失。

#1


0  

There's a buffer overflow in your socket reading code. Fix that and the problem will go away.

套接字读取代码中存在缓冲区溢出。解决这个问题,问题就会消失。