[prog] Segmentation fault :-(

Jacinta Richardson jarich at perltraining.com.au
Tue Sep 24 18:06:57 EST 2002


> The main program runs like this :
> 
>  if (GetStatus() != SOCK_HS_SUCCESS)
>                 return BaseSocket::Receive(data, dataLen, flags);
> 
> And the declaration for Receive is :
> int BaseSocket::Receive(unsigned char *buff, int len, int flags)
> {
>         return recv(sd, buff, len, flags);
> }

> Could it be that calling the method Receive requires some memory
> which causes the segfault? the variable 'data' is very large, about 66,000.

Ah, right.  Yes, if data is too big then that can certainly cause
problems.  See if you are limited in your allowed data segment size.
To do this on a *nix machine type "ulimit -a" at the prompt.  You should
get something like:

core file size (blocks)     unlimited
data seg size (kbytes)      131072
file size (blocks)          unlimited
max memory size (kbytes)    1019872
open files                  4096
pipe size (512 bytes)       8
stack size (kbytes)         2048
cpu time (seconds)          unlimited
max user processes          64
virtual memory (kbytes)     1048576


if the value in that second line is too small then every time you use more
memory than that limit you'll experience a segmentation fault.  This is of
course entirely separate from getting a segmentation fault because your
wild pointer has wandered out of bounds.

Intuitivvely it seems to me that if you're already using that much memory
then just passing the memory address shouldn't get you into any problems.
Never the less I know that I've had size issues cause me all sorts of
nasty surprises at times.

Something else to try is to add the following:

/* at the top of your program */
#include <assert.h>

/* then */
if (GetStatus() != SOCK_HS_SUCCESS)
{
    assert(data != NULL);
    return BaseSocket::Receive(data, dataLen, flags);
}

assert.h provides the assert function.  If the test is false it'll kill
your program and provide you a useful error message.  I found it a very
handy debugging technique.  Much tidier than:
if(data == NULL)
{
     fprintf(STDERR, "data is NULL at line 1234\n";
     exit(1);
}
especially because it saves you from worrying about moving line numbers.
;)

Just a quick warning though, my call to assert looks very wrong to me.
I'm telling myself that it's just my allergy to naked variables and
barewords from doing too much Perl, but I'm not sure.  It may indeed be
wrong.  If so, you'll have to read the man pages.


> Yes ,  I can cope with RTFM. :-)
> RTFM is sometimes followed by where in th FM, so that's sometimes
> helpful; Otherwise I go back and RTFM, and ignore the person who told me to!
> :-))

:)  FM references are usually provided.  You'll see.


	Jacinta


--
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson	    |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +613 9354 6001 	    |  
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
(il),-''  (li),'  ((!.-'              |   www.perltraining.com.au   |





More information about the Programming mailing list