# When my ball collide paddle, i got segmentation fault

I am trying this error for hours, checked many segmentation faults, tried various code but i can not figure out why this is happening.

breakout.c

``````//Detecting collision
GObject collision = detectCollision(window, ball);

if (collision != NULL)
{
{
velocityY = -velocityY ;
}

if (strcmp(getType(collision), brick) == 0)
{
velocityY = -velocityY;
removeGWindow(window, brick);
points = points + 1;
bricks = bricks - 1;
}

if (strcmp(getType(collision), label) == 0)
{

}

return 0;
}
``````

update: This way ball does not bounce off bricks.

``````    //Detecting collision
GObject collision = detectCollision(window, ball);

if (collision != NULL)
{
if (strcmp(getType(collision), "GRect") == 0)
{

{
velocityY = -velocityY;
}

if (collision == brick)
{
velocityY = -velocityY;
removeGWindow(window, brick);
points = points + 1;
bricks = bricks - 1;
}

if (strcmp(getType(collision), label) == 0)
{

}

}
}
``````
• what does `valgrind` tell you? – Kareem Sep 3 '15 at 10:55
• gdb always says problem is strcmp things.. specially in brick – Mearex Sep 3 '15 at 11:11

You have two misconceptions.

Misconception #1

According to `man strcmp`

`int strcmp(const char *s1, const char *s2);`

The strcmp() function compares the two strings `s1` and `s2`. It returns an integer less than, equal to, or greater than zero if `s1` is found, respectively, to be less than, to match, or be greater than `s2`.

So `strcmp` takes as inputs, two `string`s. But if you look at your second parameter, it's `paddle`, which if I am correct you have declared as a `GRect`, not a `string`. That's why you get the `segfault`.

Misconception #2

According to `getType`'s documentation

`string getType(GObject gobj);`

Returns the subtype of the object as a string, as in "`GOval`" or "`GRect`".
Usage:
`type = getType(gobj);`

So if the `ball` touches the `paddle`, `detectCollision()` will return the `GObject paddle` which you assign to `GObject collision`.

Now if you do `getType(collision)` and collision is indeed `paddle`, `getType()` will return `"GRect"`, which is a `string` of the type of the `GObject` you gave as input.

So you should `strcmp()` for `"GRect"`, not `paddle` or `brick`, and then compare `collision` to `paddle`. If the `"GRect"` touched wasn't the `paddle` it must have been a `brick`, so you act accordingly.

EDIT

Your problem lies on the following lines

``````if (collision == brick)
{
velocityY = -velocityY;
removeGWindow(window, brick);
points = points + 1;
bricks = bricks - 1;
}
``````

Unlike `initPaddle()` which returns a `GRect`, (which you later assign to `ball`), `initBricks()` has a return type of `void`, so it doesn't return anything back to keep track of the bricks in your `GWindow`.

That essentially means there is no variable `brick` for a brick. There is a

``````GRect brick = newGRect(/* ... */);
``````

probably, somewhere inside your `initBricks()` function, inside a for loop to instantiate the bricks, but you change the same `brick` variable with the latest `GRect`. You don't keep every `brick` you create, only the last (and you shouldn't). Also the reference to this last `brick` is also lost when `initBricks()` returns.

Now, even though there is no reference for the `brick`s, they are still in your GWindow, and through detect collision you can get their reference back. But you just can't use `brick`.

So every time you detect a collision with a `GRect`, first check if it was the `paddle`, if not, you don't have any other `GRect`s in your `GWindow`, so it has to be a `brick`, and hence you can just remove the `collision` from the `GWindow`.

Effectively just change this line:

``````if (collision == brick)
``````

to an

``````else
``````

If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark. And don't forget to keep coding!

• I tried your way. I added one if loop and tried this way. And this give me again same error. but when i add if (collision == paddle) { velocityY = -velocityY; } this codes instead of strcmp it worked! But I am taking this segmentation error again when ball and brick touched. Same error. Can i solve another way? I tried do not use strcmp command but this way ball didnt bounce off bricks that I dont understand why. What is the difference paddle and bricks? And thanks for misconceptions! – Mearex Sep 3 '15 at 19:08
• Hmmm. I don't get exactly what you mean. Could you update your code in your question to reflect the present state? – ChrisG Sep 3 '15 at 19:32
• I updated my code. This way it does not give me a segmentation fault but ball doesn't care bricks.. – Mearex Sep 4 '15 at 7:49
• check my edit above – ChrisG Sep 4 '15 at 8:06
• Thank you very very very much. I finally understand what is going on. You are like hero! – Mearex Sep 4 '15 at 8:20