why am I getting a segmentation fault?

John Machin sjmachin at lexicon.net
Sat Jan 22 05:34:08 CET 2005

Jay  donnell wrote:
> >### Have you looked in your database to see if the >script has
> actually
> >updated item.goodImage? Do you have a test plan?
> Thank you for the help. Sorry for the messy code. I was under time
> constraints. I had class, and I was rushing to get this working
> class. I should waited a day and read over it before I asked. Sorry
> again.

Apologise to _yourself_ for the messy code. If 'query' had been safely
tucked away as a local variable instead of a global, that problem
(typing 'query' instead of 'self.q') would have caused an exception on
the first time around.

A few points: Yes, don't rush. I've-forgotten-whom wrote something like
"Don't program standing up". Good advice.

Build things a piece at a time. Build your tests at the same time or
earlier. In this case, a stand-alone method or function that checked
that one file was OK, would have been a reasonable place to start. Then
add the code to query the db. Then add the threading stuff, gingerly.
If you build and test incrementally, then a segfault or other disaster
is highly likely to have been caused by the latest addition.

def __init__(self, url, filename, id):
self.t = time.time() <<<<<<<<<======= never used again
self.db = MySQLdb.connect(host="localhost", user="xxx",
passwd="xxx", db="xxx")
# create a cursor
self.cursor = db.cursor() <<<=== should be self.db.cursor()
#### picks up the *GLOBAL* 'db'
self.url = url
self.filename = filename
self.id = id
threadList = []
===>>>> that doesn't achieve much!

N.B. You still have two problems: (1) Your script as you said now
"seems to work". That doesn't sound like you have a test plan. (2) You
have shuffled your code around and the segfault went away; i.e. you
waved a dead chicken and the volcano stopped erupting. Most of the
changes suggested by others and myself were of a style/clarity nature.
The self.q/query thing would have caused a select instead an update;
hardly segfault territory. I wouldn't expect that busy-wait loop to
cause a segfault. You still don't know what caused the segfault. That
means you don't know how to avoid it in the future. You are still
living in the shadow of the volcano. Will the chicken trick work next

Looking forward to the next episode,

More information about the Python-list mailing list