[Tutor] Class causing segmentation fault???

Daniel Yoo dyoo@hkn.EECS.Berkeley.EDU
Fri, 21 Jul 2000 16:07:18 -0700 (PDT)

>         def __init__(self,v1Value=0.0,v2Value=0.0,v3Value=0.0):
>                 #Check and see if numbers or a vector was passed
>                 if type(v1Value)==type(1):
>                         self.v1=v1Value
>                         self.v2=v2Value
>                         self.v3=v3Value
>                 elif type(v1Value)==type(vector()):
> >>> a=xAxis
> >>> b=vector(a)
> Segmentation fault     <---- WOW!

Ok, this scared me for a second, but there's a good reason for the
segfault --- there's an infinite loop!  Let's pretend to be the
interpreter for a second:  Since the type of v1Value isn't an integer, we
go the elif.

>                 elif type(v1Value)==type(vector()):

So we try recursively constructing the default vector, with default
arguments 0.0, 0.0, 0.0, so we can do the 'type(vector())' However, here's
the tricky part:

>>> type(0.0)
<type 'float'>
>>> type(0)
<type 'int'>

Guess what happens.  Wash, rinse, repeat.

To fix this, you need to make sure that you can construct the default
vector.  It segfaulted because it ran out of memory.  (Still, this is
quite bad, and should have been checked for by the mem-allocator routine.)