This one is driving me nuts! Why would I get a seg fault when calling getType() on 'object' as returned by detectCollision()? gdb revels this is where the memory error is happening but as far as I can see I've implemented it in the same way as suggested on the pset4 problem page. Help greatly appreciated!

  • Kareem, thanks for the edits. I have a question regarding posting of code. On the psets pages, it lists "Giving or showing to a classmate your solution to a problem set’s problem when it is he or she, and not you, who is struggling to solve it." as unacceptable. I assumed if I posted my own code that will not break rules, as it is me who is looking for help. However, if someone were to use/copy my code then THEY would be in violation of the rules. @curiouskiwi helped me by looking at my code, and they did so without immediately giving an answer, which should be the normal process. – tidymonkey81 Jun 20 '14 at 4:24
  • The problem is that by posting any kind of solution code openly where prospective or current students can see it, you also would be violating the code of academic honesty. It's best to try to either give example code based on fundamental requirements of the problem than to give actual solution code. Or to exchange code privately for review when you need another set of eyes on your code. – Sean DiSanti Jun 20 '14 at 6:14

Pay close attention to what detectCollision() returns. What happens when there is no collision? (which will be most of the time). Are you handling that? It appears you are not, hence your segfault.

  • Okay, so it returns a GObject and I assume if it doesn't detect a collision then object == NULL... but i'm not sure why i have to handle that case, seeing as detectcollision will just go ahead and return NULL without any effect on the rest of the code. what am i missing? and thanks for your quick response – tidymonkey81 Jun 19 '14 at 22:44
  • Your code says if strcmp(getType(object) .... what might happen if getType() is passed NULL? Would you expect that to work? – curiouskiwi Jun 19 '14 at 23:41
  • oh okay, i assumed that object was the variable and that,for instance, object = paddle, or object = brick or object = NULL. i will go back and have a look at what i can do. – tidymonkey81 Jun 19 '14 at 23:48
  • It returns Object if there is a collision, otherwise it returns NULL. (in other words, it doesn't return a GObject). Perhaps just a test to make sure that object isn't null? – curiouskiwi Jun 19 '14 at 23:54
  • thanks a lot for your help. i guess i didn't see that it would return anything BUT object. for the record, are you able to tell me where the documentation is for detectCollision()? – tidymonkey81 Jun 20 '14 at 0:17

@kevin, there is no documentation for detectCollision() as it is not a predefined function in the Stanford cslib package. Rather, it was written expressly for the pset and is the last function defined at the bottom of breakout.c. I think you'll find the answer to your problem there.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .