Run the program under gdb and see which function was calles before. It should allow you to find where more or less you have a problem. It's hard to help moe without having the code.
How to use gdb:
gdb progname
run
(when you get segfault, run

bt
(it lists functions called, find first 'yours', that's the one that causes the problem probably)