2

I am having a problem with the updateScoreboard function in pset4. Whenever I call it I get a Segmentation fault (core dumped) error.

My code is the following:

if(object!=NULL && strcmp(getType(object), "GRect") == 0 && object!=paddle)
{
    velocity_Y = -velocity_Y;
    removeGWindow(window,object);
    points++;
    bricks--;
    updateScoreboard(window,label,points);
}

In addition to Segmentation fault (core dumped) I also receive error messages that don't affect the game, like:

java.lang.ClassCastException: stanford.spl.JBELabel cannot be cast to acm.graphics.GFillable
    at stanford.spl.GObject_setFilled.execute(JBECommand.java:978)
    at stanford.spl.JavaBackEnd.commandLoop(JavaBackEnd.java:625)
    at stanford.spl.JavaBackEnd.run(JavaBackEnd.java:164)
    at stanford.spl.JavaBackEnd.main(JavaBackEnd.java:136)
Unexpected error: stanford.spl.JBELabel cannot be cast to acm.graphics.GFillable`

Here is the output of valgrind ./breakout:

==17374== Invalid write of size 4
==17374==    at 0x805021D: setLabel (in /home/jharvard/Dropbox/pset4/breakout)
==17374==    by 0x8049E9D: updateScoreboard (breakout.c:228)
==17374==    by 0x8049574: main_ (breakout.c:81)
==17374==    by 0x804A391: main (in /home/jharvard/Dropbox/pset4/breakout)
==17374==  Address 0x3c is not stack'd, malloc'd or (recently) free'd
==17374== 
==17374== 
==17374== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==17374==  Access not within mapped region at address 0x3C
==17374==    at 0x805021D: setLabel (in /home/jharvard/Dropbox/pset4/breakout)
==17374==    by 0x8049E9D: updateScoreboard (breakout.c:228)
==17374==    by 0x8049574: main_ (breakout.c:81)
==17374==    by 0x804A391: main (in /home/jharvard/Dropbox/pset4/breakout)
==17374==  If you believe this happened as a result of a stack
==17374==  overflow in your program's main thread (unlikely but
==17374==  possible), you can try to increase the size of the
==17374==  main thread stack using the --main-stacksize= flag.
==17374==  The main thread stack size used in this run was 8388608.
==17374== 
==17374== HEAP SUMMARY:
==17374==     in use at exit: 94,916 bytes in 551 blocks
==17374==   total heap usage: 666 allocs, 115 frees, 104,818 bytes allocated
==17374== 
==17374== LEAK SUMMARY:
==17374==    definitely lost: 8,737 bytes in 267 blocks
==17374==    indirectly lost: 78,182 bytes in 214 blocks
==17374==      possibly lost: 7,293 bytes in 68 blocks
==17374==    still reachable: 704 bytes in 2 blocks
==17374==         suppressed: 0 bytes in 0 blocks
==17374== Rerun with --leak-check=full to see details of leaked memory
==17374== 
==17374== For counts of detected and suppressed errors, rerun with: -v
==17374== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault

And here's the function updateScorebaord():

void updateScoreboard(GWindow window, GLabel label, int points)
{
    // update label
    char s[12];
    sprintf(s, "%i", points);
    setLabel(label, s);

    // center label in window
    double x = (getWidth(window) - getWidth(label)) / 2;
    double y = (getHeight(window) - getHeight(label)) / 2;
    setLocation(label, x, y);
6
  • Please run valgrind ./breakout and paste the results here. – Luke Van In Aug 12 '14 at 23:05
  • Okay, I've updated the question and the result is there ! – Kheyreddine Hadri Aug 12 '14 at 23:11
  • It's crashing on updateScoreboard() around line 228. Can you edit to show that line plus the ones before/after? – curiouskiwi Aug 12 '14 at 23:14
  • Okay, edited! I added the updateScoreboard and where I've called it. Line 228 is sprintf(s, "%i", points); – Kheyreddine Hadri Aug 12 '14 at 23:22
  • Well I'm stumped. Try change that line to snprintf(s, 12, "%d", points);. – Luke Van In Aug 12 '14 at 23:38
4

Thankfully I found where I have done a mistake!

It's in the initScoreboard . I didn't add return scoreboard to be used in updateScoreboard because in the beginning of main there's GLabel label=initScoreboard and that must have a return value! Thank you for your help ^^

1
  • 2
    Nice one! :) Remember to mark your answer as "accepted". – Luke Van In Aug 13 '14 at 11:39

You must log in to answer this question.

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