5

For pset4, in this code(in main, to check collision of ball and anything else):

GObject object = detectCollision(window, ball);

        if (strcmp(getType(object), "GRect") == 0)
        {
            if (object == paddle)
            {
                velocityY *= -1;
            }
        }

On compiling returns, "Segmentation Fault, core dumped"

SO, I ran valgrind, and here's the result:

==4188== Memcheck, a memory error detector
==4188== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==4188== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==4188== Command: ./breakout
==4188== 
==4188== Invalid read of size 4
==4188==    at 0x804ECE3: getType (in /home/jharvard/Dropbox/pset4/breakout)
==4188==    by 0x8049786: main_ (breakout.c:94)
==4188==    by 0x804A0FD: main (in /home/jharvard/Dropbox/pset4/breakout)
==4188==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==4188== 
==4188== 
==4188== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==4188==  Access not within mapped region at address 0x0
==4188==    at 0x804ECE3: getType (in /home/jharvard/Dropbox/pset4/breakout)
==4188==    by 0x8049786: main_ (breakout.c:94)
==4188==    by 0x804A0FD: main (in /home/jharvard/Dropbox/pset4/breakout)
==4188==  If you believe this happened as a result of a stack
==4188==  overflow in your program's main thread (unlikely but
==4188==  possible), you can try to increase the size of the
==4188==  main thread stack using the --main-stacksize= flag.
==4188==  The main thread stack size used in this run was 8388608.
==4188== 
==4188== HEAP SUMMARY:
==4188==     in use at exit: 94,800 bytes in 549 blocks
==4188==   total heap usage: 612 allocs, 63 frees, 101,682 bytes allocated
==4188== 
==4188== LEAK SUMMARY:
==4188==    definitely lost: 8,717 bytes in 266 blocks
==4188==    indirectly lost: 78,182 bytes in 214 blocks
==4188==      possibly lost: 7,197 bytes in 67 blocks
==4188==    still reachable: 704 bytes in 2 blocks
==4188==         suppressed: 0 bytes in 0 blocks
==4188== Rerun with --leak-check=full to see details of leaked memory
==4188== 
==4188== For counts of detected and suppressed errors, rerun with: -v
==4188== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault

I'm no expert at reading Valgrind, but as far as I understood, it means to tell me that something's wrong with getType through these lines:

==4188== Invalid read of size 4
==4188==    at 0x804ECE3: getType (in /home/jharvard/Dropbox/pset4/breakout)
==4188==    by 0x8049786: main_ (breakout.c:94)
==4188==    by 0x804A0FD: main (in /home/jharvard/Dropbox/pset4/breakout)
==4188==  Address 0x0 is not stack'd, malloc'd or (recently) free'd

But I have checked that i'm not making any mistakes while using that, so what exactly is going on here ?

Thanks in advance

19

The function detectCollision() returns NULL if there's no collision happened. You have to check whether your object is equal to NULL before you pass it to getType().

| improve this answer | |
  • Thank you. I was stuck on this as well. – John Douglass Sep 28 '14 at 4:38
  • Thanks a lot as well, solved my issue. That's what happens when you don't check things properly :). – TsvetaB Apr 2 '15 at 12:19
  • Thanks, i was getting a segmentation fault (core dumped). your solution was really helpful. – Luwii Sep 24 '15 at 20:44

You must log in to answer this question.

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