Something weird is happening with my software.
I have a thread wich reads a file and write to a RM.
This thread have a while(1) loop and is blocked using pthread_cond_wait();
The first time the thread is unblocked, it first open 2 files (fd_cfg=3 and fd_rm=4), then I make a read() call in fd_cfg and a write() in fd_rm, then block again.
The next time the thread is unblocked, this variables have different values (as if it was accessing invalid memory address) and I got an error (of course). But, I’m sure I’m not changing this values anywhere in the program.
I tried to make fd_cfg and fd_rm as global variables and test again, this time the values looks right (3 and 4) but gdb returned me a SIGTRAP fault!

Any idea?

I’m using QNX 6.1 - gcc 2.95.2

Leandro Colen

Problem solved.

I’ve got this SIGTRAP error only a few times. After some tests, this SIGTRAP was changed to SIGSEGV (segmentation fault).

The problem happens when I use a struct :

struct test { int a; int b; char c[SIZE]; };

When you have this array as the last thing in struct and send this struct to RM (via write()), some variables get weird values when the program unblocks.

To solve :

struct test { int a; int b; char c[SIZE]; int not_used; };

Now, everything is back to normal.


No, I doubt you fixed the problem, but rather moved it somewhere else. This is called programming by coincidence and isn’t a solution.

You appear to have a problem with either a pointer, or perhaps you are failing to NUL terminate your string and now you “just” happen to do that by storing an int right after it.


OK… Good point.

But, I understand that, if I declare a array with a fixed size, the compiler will allocate the memory to store the array. So I dont have to worry about it. If I overflow the array, the program reports a run-time error (buffer overflow, segmentation error, etc) and terminates.

But, what happens is, I fill the the struct, and send it to RM.
The RM reads the message, test it, and reply to the client. At this point, when the client unblocks, a variable (in this case a file descriptor) have its memory region “invaded” somehow and no error was reported, signal received, or whatever.
So, where is the problem? The compiler seams to works fine (an overflow exception is reported if i overflow the buffer, so i don’t :slight_smile:) but between the reply of RM and unblock of client a memory region from the client is “invaded” and modified.

With this in mind I’ve decided to “delimit” the memory region for this struct storing an int after the array.

I understand your point, but why this problem results a memory “invasion”? It should return an error when it happens.
The program continues to work fine until I use the variable again.

It was quite difficult to find the problem and then to look for a solution.

If you can post source, we can look at it and find the error you are doing :slight_smile:

Not quite. Unlike some languages, C does not have runtime error checking for things like buffer overflows. If you happen to be at the end of a selector, you will get a segment violation if you overflow the buffer. Otherwise you will just trash whatever follows in memory.

The problem is that you are treating a symptom of the problem, not the source of the problem. You managed to stop it from breaking there, without understanding why (read the reference to programming by coincidence). So when you change it some more (add new features, etc), the symptom will show up somewhere else.

Well, you haven’t found the problem yet, you just managed to make it move to somewhere else (which you haven’t seen yet).


After your help, I do understand now what do you mean by moved the problem. It happened again. :open_mouth:

I’ll start looking for the problem, but if you have any tips to guide me, it will be most welcome. :slight_smile:

Thanx for the help.