'Building with CMake gives segfault on strlen
I am creating a game in SDL2 and while everything mostly works, whenever I build with CMake, I can't open any (image) file with a string, because I get a segmentation fault. I happen to also have a Makefile available which doesn't give the same result (It works fine).
The function I'm calling as declared:
extern SDL_Texture* RenderSomething(int x, int y, float space, char** path, SDL_Renderer* Renderer);
(I've changed it to a double pointer to see if it was a problem).
The code I'm calling it as:
RenderSomething(mouseX, mouseY, size, &__path__, Renderer);
Options I use in the Makefile:
-Wno-switch-default -pedantic -Wall -Wextra -Wcast-align -Wcast-qual -Wdisabled-optimization -Wformat=2 -Winit-self -Wlogical-op -Wmissing-declarations -Wmissing-include-dirs -Wredundant-decls -Wsign-conversion -Wstrict-overflow=5 -Wundef -Wno-unused -Wno-implicit-fallthrough -Wall -Wextra -O5
Declaration of __path__:
char* __path__ = "/the/path/to/my/image.png";
The segmentation fault as described by gdb:
Thread 1 "app.bin" received signal SIGSEGV, Segmentation fault.
0x00007ffff7a474bd in __strlen_avx2 () from /usr/lib/libc.so.6
The implementation of RenderSomething:
SDL_Texture* RenderSomething(int x, int y, float space, char** path, SDL_Renderer* Renderer) {
SDL_Rect rect;
SDL_Surface* srf = IMG_Load(*path);
SDL_Texture* texture = NULL;
if (!srf) {
/* insert error handling code here */
}
else {
texture = SDL_CreateTextureFromSurface(Renderer, srf);
SDL_FreeSurface(srf);
if (!texture) {
/* insert even more error handling here */
} else {
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
rect.h = 32 * space / 2; // Every image is 32x32, so we don't bother with querying the dimensions of it.
rect.w = 32 * space / 2;
rect.x = x;
rect.y = y;
SDL_RenderCopy(Renderer, texture, NULL, &rect);
}
}
return texture;
}
Solution 1:[1]
Turns out the error was caused by a nearby #ifdef statement.. Which i had enabled in CMake. I don't know how did it happen but it did. Thanks to @u-235 for the advice, since the code was actually screwed up by nearby symbols :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | AggelosT |
