orangeduck/BuildYourOwnLisp

builtin_load causes segfault

Closed this issue · 3 comments

So, I'm on chapter 14 now, and I cannot for the life of me figure out this error I'm getting.
I finished the chapter, and everything seems to be working fine except the load function - whenever I use it I get a segfault.
I quickly fired up gdb, and the error message is:
" Program received signal SIGSEGV, Segmentation fault.
0x00000000004070fb in mpc_parse_input (i=0x63a210, init=0x0, final=0x7fffffffe570) at mpc.c:983
983 switch (p->type) { "
I then compiled and ran your completed version through gdb to inspect the variables up to that point and I see that init appears to be the parser pointer to Lispy, as opposed to the NULL my program gives up.
I've compared our code and it's mostly the same save for a few things that i've added in previous chapters, but those shouldn't cause problems seeing as the language works fine outside of the load function (is this a correct assumption?).
Any help would be appreciated.

EDIT: I'm on linux, and I have the code in a repo here, although I haven't updated it in a while since I'm mostly writing this for myself. If necessary, I can commit the latest changes.

Hi Lucantrop,

It looks like you identified the problem - Lispy shouldn't be NULL when it is getting passed to that function. Make sure you forward declare all your parser functions (such as Lispy) so they are in the outermost scope of the program, not local to main (described in the "Load Function" section of the chapter. Then Lispy and all the other parsers should be created as one of the first things done E.G Lispy = mpc_new("lispy");. Make sure this is happening.

Finally it also may be important to make sure mpc_cleanup is not called too early (i.e before a call to the builtin_load function). This could be happening if you've changed things.

The basic ideas is that you need to make sure Lispy is created and not destroyed before builtin_load is called. You can use gdb or printf to see what it's value is at different points in the program.

Hope that helps,

Dan

Ah, of course it was something as obvious as this: I forgot to delete the type declaration to the parser pointers in my main function (mpc_parser_t* Lispy instead of just Lispy) so the forward declarations at the top of the file were getting overwritten.

Thank you very much for your quick reply,
Luca

No problem. Glad to help!