problem with SIGSEGV

Hi!

I´m doing my first application, and I have a problem with SIGSEGV signal.

I debug my application and it runs correctly for hours, but when least
expected was caused an segmentation fault (SIGSEGV signal).
This error was produced when I try to write in a file.

The time the application is running is random (2, 3 , … 10, 11 or 12, …,
15 hours).

Can someone help me? Do you need the code?

Jorge

If you are running QNX4 with Watcom you can start the dumper utility to give
you a dump file when a process terminates, and you can then use the Watcom
debugger to find what line of the program caused the segment violation.


C. Scott

“Jorge Alonso” <jalonso@ain.es> wrote in message
news:bu0dq9$hqv$1@inn.qnx.com

Hi!

I´m doing my first application, and I have a problem with SIGSEGV signal.

I debug my application and it runs correctly for hours, but when least
expected was caused an segmentation fault (SIGSEGV signal).
This error was produced when I try to write in a file.

The time the application is running is random (2, 3 , … 10, 11 or 12,
…,
15 hours).

Can someone help me? Do you need the code?

Jorge

I forgot to mention that with QNX 6 (which is what you are probably running)
you should get a core file when a process crashes, and you can use gdb to
see what line caused the crash.


C. Scott




“C. Scott” <cscott@nospam.com> wrote in message
news:bu0nlh$n17$1@nntp.qnx.com

If you are running QNX4 with Watcom you can start the dumper utility to
give
you a dump file when a process terminates, and you can then use the Watcom
debugger to find what line of the program caused the segment violation.


C. Scott

“Jorge Alonso” <> jalonso@ain.es> > wrote in message
news:bu0dq9$hqv$> 1@inn.qnx.com> …
Hi!

I´m doing my first application, and I have a problem with SIGSEGV
signal.

I debug my application and it runs correctly for hours, but when least
expected was caused an segmentation fault (SIGSEGV signal).
This error was produced when I try to write in a file.

The time the application is running is random (2, 3 , … 10, 11 or 12,
…,
15 hours).

Can someone help me? Do you need the code?

Jorge
\

C. Scott <cscott@nospam.com> wrote:
CS > I forgot to mention that with QNX 6 (which is what you are probably running)
CS > you should get a core file when a process crashes, and you can use gdb to
CS > see what line caused the crash.

Also, be aware that you are probibly dereferencing a pointer that is
not (no longer) valid.

This line ‘buffer = (unsigned char)malloc(80);’ in FileCp() does fishy
casting to (unsigned char) instead of (unsigned char *). As result
‘buffer’ variable may point to somewhere else but not where you expect
it to. Sometimes you may damage data at that ‘somewhere’ location. And
maybe that’s what causes the problem in WriteVibrations(). Sometimes
your ‘buffer’ pointer can not be dereferenced atall and you get SIGSEGV
at ‘fwrite(buffer, 1, bytes, pfd);’ line.

The line should be:
buffer = (unsigned char *)malloc(80);

-dp

Jorge Alonso wrote:

Hi!

First, I work with QNX 6.
Then, I know the function and line where is caused the crash. Always it is
produced in one of this functions (although in most cases it´s in the
first):

void WriteVibrations (FILE * f)
{
unsigned short i;
int vibr;
int *pVibr;
float mg, mv;

pVibr = &vibr;
for (i=0; i<NUM_CONVERSIONS; i++) {
mv = (5000 * g_aBufferVibr[g_nChannel] > / 32768.0);
mg = (1000 * mv) / g_nSensitivity[g_nChannel];
vibr = (int)mg;
fwrite(pVibr, sizeof(int), 1, f); //here SIGSEGV
signal is produced!!!
}
}

int FileCp(char *szSourceFile, char *szDestFile)
{
FILE *pfo;
FILE *pfd;
int bytes;
unsigned char *buffer;

buffer = (unsigned char)malloc(80);
if (buffer != NULL) {
pfo = fopen(szSourceFile, “rb”)
if (pfo != NULL) {
pfd = fopen(szDestFile, “wb”)
if (pfd != NULL) {
while(1) {
bytes = fread(buffer, 1, 80, pfo);
if (bytes == 0) break;
fwrite(buffer, 1, bytes, pfd); //or here
SIGSEGV signal is produced!!!
}
fclose(pfd);
}
else {
fclose(pfo);
free(buffer);
return 1;
}
fclose(pfo);
}
else {
free(buffer);
return 1;
}
free(buffer);
}
return 0;
}

I think you want to say this:

FILE *f;
int *p;


fwrite(p, sizeof(char), 1, f); //the variable´s size doesn´t valid


when you say “you are probibly dereferencing a pointer that is not (no
longer) valid”. Is it the case? If not, Can you explain me with an example?



“Bill Caroselli” <> qtps@earthlink.net> > escribió en el mensaje
news:bu0ssp$s31$> 2@inn.qnx.com> …

C. Scott <> cscott@nospam.com> > wrote:
CS > I forgot to mention that with QNX 6 (which is what you are probably

running)

CS > you should get a core file when a process crashes, and you can use

gdb to

CS > see what line caused the crash.

Also, be aware that you are probibly dereferencing a pointer that is
not (no longer) valid.

Hi!

First, I work with QNX 6.
Then, I know the function and line where is caused the crash. Always it is
produced in one of this functions (although in most cases it´s in the
first):

void WriteVibrations (FILE * f)
{
unsigned short i;
int vibr;
int *pVibr;
float mg, mv;

pVibr = &vibr;
for (i=0; i<NUM_CONVERSIONS; i++) {
mv = (5000 * g_aBufferVibr[g_nChannel] / 32768.0);
mg = (1000 * mv) / g_nSensitivity[g_nChannel];
vibr = (int)mg;
fwrite(pVibr, sizeof(int), 1, f); //here SIGSEGV
signal is produced!!!
}
}

int FileCp(char *szSourceFile, char *szDestFile)
{
FILE *pfo;
FILE *pfd;
int bytes;
unsigned char *buffer;

buffer = (unsigned char)malloc(80);
if (buffer != NULL) {
pfo = fopen(szSourceFile, “rb”)
if (pfo != NULL) {
pfd = fopen(szDestFile, “wb”)
if (pfd != NULL) {
while(1) {
bytes = fread(buffer, 1, 80, pfo);
if (bytes == 0) break;
fwrite(buffer, 1, bytes, pfd); //or here
SIGSEGV signal is produced!!!
}
fclose(pfd);
}
else {
fclose(pfo);
free(buffer);
return 1;
}
fclose(pfo);
}
else {
free(buffer);
return 1;
}
free(buffer);
}
return 0;
}

I think you want to say this:

FILE *f;
int *p;


fwrite(p, sizeof(char), 1, f); //the variable´s size doesn´t valid


when you say “you are probibly dereferencing a pointer that is not (no
longer) valid”. Is it the case? If not, Can you explain me with an example?



“Bill Caroselli” <qtps@earthlink.net> escribió en el mensaje
news:bu0ssp$s31$2@inn.qnx.com

C. Scott <> cscott@nospam.com> > wrote:
CS > I forgot to mention that with QNX 6 (which is what you are probably
running)
CS > you should get a core file when a process crashes, and you can use
gdb to
CS > see what line caused the crash.

Also, be aware that you are probibly dereferencing a pointer that is
not (no longer) valid.

Jorge Alonso wrote:

Sorry, I forgot to write ‘*’ in the line 'buffer = (unsigned char
*)malloc(80);

Can you explain me the meaning of ‘dereferencing a pointer’?

http://www.google.ca/search?q=dereferencing+pointer+C++programming&ie=UTF-8&oe=UTF-8&hl=en&btnG=Google+Search&meta=


Thanks.


“Dmitri Poustovalov” <> pdmitri@bbbiiigggfffoooooottt.com> > escribió en el
mensaje news:> 40050A5D.80502@bbbiiigggfffoooooottt.com> …

This line ‘buffer = (unsigned char)malloc(80);’ in FileCp() does fishy
casting to (unsigned char) instead of (unsigned char *). As result
‘buffer’ variable may point to somewhere else but not where you expect
it to. Sometimes you may damage data at that ‘somewhere’ location. And
maybe that’s what causes the problem in WriteVibrations(). Sometimes
your ‘buffer’ pointer can not be dereferenced atall and you get SIGSEGV
at ‘fwrite(buffer, 1, bytes, pfd);’ line.

The line should be:
buffer = (unsigned char *)malloc(80);

-dp

Jorge Alonso wrote:

Hi!

First, I work with QNX 6.
Then, I know the function and line where is caused the crash. Always it

is

produced in one of this functions (although in most cases it´s in the
first):

void WriteVibrations (FILE * f)
{
unsigned short i;
int vibr;
int *pVibr;
float mg, mv;

pVibr = &vibr;
for (i=0; i<NUM_CONVERSIONS; i++) {
mv = (5000 * g_aBufferVibr[g_nChannel] > / 32768.0);
mg = (1000 * mv) / g_nSensitivity[g_nChannel];
vibr = (int)mg;
fwrite(pVibr, sizeof(int), 1, f); //here

SIGSEGV

signal is produced!!!
}
}

int FileCp(char *szSourceFile, char *szDestFile)
{
FILE *pfo;
FILE *pfd;
int bytes;
unsigned char *buffer;

buffer = (unsigned char)malloc(80);
if (buffer != NULL) {
pfo = fopen(szSourceFile, “rb”)
if (pfo != NULL) {
pfd = fopen(szDestFile, “wb”)
if (pfd != NULL) {
while(1) {
bytes = fread(buffer, 1, 80, pfo);
if (bytes == 0) break;
fwrite(buffer, 1, bytes, pfd); //or

here

SIGSEGV signal is produced!!!
}
fclose(pfd);
}
else {
fclose(pfo);
free(buffer);
return 1;
}
fclose(pfo);
}
else {
free(buffer);
return 1;
}
free(buffer);
}
return 0;
}

I think you want to say this:

FILE *f;
int *p;


fwrite(p, sizeof(char), 1, f); //the variable´s size doesn´t

valid



when you say “you are probibly dereferencing a pointer that is not (no
longer) valid”. Is it the case? If not, Can you explain me with an

example?



“Bill Caroselli” <> qtps@earthlink.net> > escribió en el mensaje
news:bu0ssp$s31$> 2@inn.qnx.com> …


C. Scott <> cscott@nospam.com> > wrote:
CS > I forgot to mention that with QNX 6 (which is what you are probably

running)


CS > you should get a core file when a process crashes, and you can use

gdb to


CS > see what line caused the crash.

Also, be aware that you are probibly dereferencing a pointer that is
not (no longer) valid.



\

Sorry, I forgot to write ‘*’ in the line 'buffer = (unsigned char
*)malloc(80);

Can you explain me the meaning of ‘dereferencing a pointer’?

Thanks.


“Dmitri Poustovalov” <pdmitri@bbbiiigggfffoooooottt.com> escribió en el
mensaje news:40050A5D.80502@bbbiiigggfffoooooottt.com

This line ‘buffer = (unsigned char)malloc(80);’ in FileCp() does fishy
casting to (unsigned char) instead of (unsigned char *). As result
‘buffer’ variable may point to somewhere else but not where you expect
it to. Sometimes you may damage data at that ‘somewhere’ location. And
maybe that’s what causes the problem in WriteVibrations(). Sometimes
your ‘buffer’ pointer can not be dereferenced atall and you get SIGSEGV
at ‘fwrite(buffer, 1, bytes, pfd);’ line.

The line should be:
buffer = (unsigned char *)malloc(80);

-dp

Jorge Alonso wrote:
Hi!

First, I work with QNX 6.
Then, I know the function and line where is caused the crash. Always it
is
produced in one of this functions (although in most cases it´s in the
first):

void WriteVibrations (FILE * f)
{
unsigned short i;
int vibr;
int *pVibr;
float mg, mv;

pVibr = &vibr;
for (i=0; i<NUM_CONVERSIONS; i++) {
mv = (5000 * g_aBufferVibr[g_nChannel] > / 32768.0);
mg = (1000 * mv) / g_nSensitivity[g_nChannel];
vibr = (int)mg;
fwrite(pVibr, sizeof(int), 1, f); //here
SIGSEGV
signal is produced!!!
}
}

int FileCp(char *szSourceFile, char *szDestFile)
{
FILE *pfo;
FILE *pfd;
int bytes;
unsigned char *buffer;

buffer = (unsigned char)malloc(80);
if (buffer != NULL) {
pfo = fopen(szSourceFile, “rb”)
if (pfo != NULL) {
pfd = fopen(szDestFile, “wb”)
if (pfd != NULL) {
while(1) {
bytes = fread(buffer, 1, 80, pfo);
if (bytes == 0) break;
fwrite(buffer, 1, bytes, pfd); //or
here
SIGSEGV signal is produced!!!
}
fclose(pfd);
}
else {
fclose(pfo);
free(buffer);
return 1;
}
fclose(pfo);
}
else {
free(buffer);
return 1;
}
free(buffer);
}
return 0;
}

I think you want to say this:

FILE *f;
int *p;


fwrite(p, sizeof(char), 1, f); //the variable´s size doesn´t
valid


when you say “you are probibly dereferencing a pointer that is not (no
longer) valid”. Is it the case? If not, Can you explain me with an
example?



“Bill Caroselli” <> qtps@earthlink.net> > escribió en el mensaje
news:bu0ssp$s31$> 2@inn.qnx.com> …

C. Scott <> cscott@nospam.com> > wrote:
CS > I forgot to mention that with QNX 6 (which is what you are probably

running)

CS > you should get a core file when a process crashes, and you can use

gdb to

CS > see what line caused the crash.

Also, be aware that you are probibly dereferencing a pointer that is
not (no longer) valid.


\

Thanks, Jorge



“Dmitri Poustovalov” <pdmitri@bbbiiigggfffoooooottt.com> escribió en el
mensaje news:4007A7FF.9090308@bbbiiigggfffoooooottt.com

Jorge Alonso wrote:
Sorry, I forgot to write ‘*’ in the line 'buffer = (unsigned char
*)malloc(80);

Can you explain me the meaning of ‘dereferencing a pointer’?


http://www.google.ca/search?q=dereferencing+pointer+C++programming&ie=UTF-8> &

oe=UTF-8&hl=en&btnG=Google+Search&meta=

Thanks.


“Dmitri Poustovalov” <> pdmitri@bbbiiigggfffoooooottt.com> > escribió en el
mensaje news:> 40050A5D.80502@bbbiiigggfffoooooottt.com> …

This line ‘buffer = (unsigned char)malloc(80);’ in FileCp() does fishy
casting to (unsigned char) instead of (unsigned char *). As result
‘buffer’ variable may point to somewhere else but not where you expect
it to. Sometimes you may damage data at that ‘somewhere’ location. And
maybe that’s what causes the problem in WriteVibrations(). Sometimes
your ‘buffer’ pointer can not be dereferenced atall and you get SIGSEGV
at ‘fwrite(buffer, 1, bytes, pfd);’ line.

The line should be:
buffer = (unsigned char *)malloc(80);

-dp

Jorge Alonso wrote:

Hi!

First, I work with QNX 6.
Then, I know the function and line where is caused the crash. Always it

is

produced in one of this functions (although in most cases it´s in the
first):

void WriteVibrations (FILE * f)
{
unsigned short i;
int vibr;
int *pVibr;
float mg, mv;

pVibr = &vibr;
for (i=0; i<NUM_CONVERSIONS; i++) {
mv = (5000 * g_aBufferVibr[g_nChannel] > / 32768.0);
mg = (1000 * mv) / g_nSensitivity[g_nChannel];
vibr = (int)mg;
fwrite(pVibr, sizeof(int), 1, f); //here

SIGSEGV

signal is produced!!!
}
}

int FileCp(char *szSourceFile, char *szDestFile)
{
FILE *pfo;
FILE *pfd;
int bytes;
unsigned char *buffer;

buffer = (unsigned char)malloc(80);
if (buffer != NULL) {
pfo = fopen(szSourceFile, “rb”)
if (pfo != NULL) {
pfd = fopen(szDestFile, “wb”)
if (pfd != NULL) {
while(1) {
bytes = fread(buffer, 1, 80, pfo);
if (bytes == 0) break;
fwrite(buffer, 1, bytes, pfd); //or

here

SIGSEGV signal is produced!!!
}
fclose(pfd);
}
else {
fclose(pfo);
free(buffer);
return 1;
}
fclose(pfo);
}
else {
free(buffer);
return 1;
}
free(buffer);
}
return 0;
}

I think you want to say this:

FILE *f;
int *p;


fwrite(p, sizeof(char), 1, f); //the variable´s size doesn´t

valid



when you say “you are probibly dereferencing a pointer that is not (no
longer) valid”. Is it the case? If not, Can you explain me with an

example?



“Bill Caroselli” <> qtps@earthlink.net> > escribió en el mensaje
news:bu0ssp$s31$> 2@inn.qnx.com> …


C. Scott <> cscott@nospam.com> > wrote:
CS > I forgot to mention that with QNX 6 (which is what you are
probably

running)


CS > you should get a core file when a process crashes, and you can
use

gdb to


CS > see what line caused the crash.

Also, be aware that you are probibly dereferencing a pointer that is
not (no longer) valid.





\