SDL memory leak - c

So I tryed to make something on SDL, but on first programm I have memory lear (idk leak or not) so there is some code:
#include <stdio.h>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#define SCREENSIZEX 180
#define SCREENSIZEY 300
SDL_Window* mainwind = NULL;
SDL_Renderer* rend = NULL;
TTF_Font* Usefont = NULL;
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
Uint32 windowflags;
windowflags = SDL_WINDOW_SHOWN;
mainwind = SDL_CreateWindow("FooBar",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
SCREENSIZEX,
SCREENSIZEY,
windowflags);
rend = SDL_CreateRenderer(mainwind, -1, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(rend, 255, 255, 255, 255);
int imgFlags = IMG_INIT_PNG;
IMG_Init(imgFlags);
TTF_Init();
Usefont = TTF_OpenFont("DOTMBold.TTF",90);
SDL_Surface* TextSurf = NULL;
SDL_Texture* TextTexture = NULL;
SDL_Color UsingColor;
UsingColor.r=0;
UsingColor.g=255;
UsingColor.b=255;
UsingColor.a=100;
bool exit = false;
char Text[500];
int counter = 0;
SDL_Event evneet;
while(!exit)
{
SDL_PollEvent(&evneet);
SDL_RenderClear(rend);
counter++;
TextSurf = TTF_RenderUTF8_Blended(Usefont, Text, UsingColor);
TextTexture = SDL_CreateTextureFromSurface(rend, TextSurf);
SDL_FreeSurface(TextSurf);
TextSurf = NULL;
SDL_RenderCopy(rend, TextTexture, NULL, NULL);
TextTexture = NULL;
SDL_DestroyTexture(TextTexture);
SDL_RenderPresent(rend);
}
SDL_FreeSurface(TextSurf);
TextSurf = NULL;
SDL_DestroyTexture(TextTexture);
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(mainwind);
SDL_Quit();
return 0;
}
Problem:
some screenshots
Idk how to fix this and tryed to do a lot of freeing and memory manipulations.
This programm do only one task. Just counting frames (in code only 0 displayed)
Its 3rd my try to make rendering and always i got the same.
Please help!

This looks suspicious:
while(!exit)
{
...
TextTexture = SDL_CreateTextureFromSurface(rend, TextSurf);
...
TextTexture = NULL; // A
SDL_DestroyTexture(TextTexture); // B
...
}
SDL_DestroyTexture() doesn't get a valid handle here, but a NULL-Pointer is passed. You have to swap the lines A and B, so the Texture can be freed properly.

The answer was strange. Thanks Ctx thats first part of this big mess with rerenderring surfaces and textuers. As for me big mistake was NULLing texture when i need to pass "nullptr".
SDL_FreeSurface(AlreadyDrawedAndUsedOnSurface);
AlreadyDrawedAndUsedOnSurface = NULL; //BAD!
AlreadyDrawedAndUsedOnSurface = nullptr; //good)
Very strange for me but it works!

Related

vkCreateInsatance segmentation fault

I followed by vulkan tutorial, and i get segfault on vkCreateInstance.
I use arch (btw), and i install vulkan by paru -S vulkan-devel (also linux-firmware and mesa are installed). My CPU is AMD Ryzen 7 2700, and GPU is Radeon RX 580 4GB.
I saw a similar question, but the answer did not solve my problem (and besides, they use C++, not C)
#include <stdio.h>
#include <stdlib.h>
#include <vulkan/vulkan.h>
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
const char* APPLICATION_NAME = "Vulkan Teach";
const uint16_t WIDTH = 800;
const uint16_t HEIGHT = 600;
static GLFWwindow* window;
static VkInstance instance;
void error(char* message, int code) {
fprintf(stderr, "%s (status code: %d)\n", message, code);
exit(code);
}
void initWindow() {
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
window = glfwCreateWindow(WIDTH, HEIGHT, APPLICATION_NAME, NULL, NULL);
}
void mainLoop() {
while(!glfwWindowShouldClose(window)) {
glfwPollEvents();
}
}
void createInsance() {
VkApplicationInfo appInfo;
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pApplicationName = APPLICATION_NAME;
appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.pEngineName = "No Engine";
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.apiVersion = VK_API_VERSION_1_0;
VkInstanceCreateInfo createInfo;
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
createInfo.pApplicationInfo = &appInfo;
uint32_t glfwExtensionsCount = 0;
const char** glfwExtensions;
glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionsCount);
createInfo.enabledExtensionCount = glfwExtensionsCount;
createInfo.ppEnabledExtensionNames = glfwExtensions;
createInfo.enabledLayerCount = 0;
VkResult result = vkCreateInstance(&createInfo, NULL, &instance);
if(result != VK_SUCCESS) {
error("Failed to create vulkan instance.", result);
}
}
void initVulkan() {
createInsance();
}
void cleanup() {
vkDestroyInstance(instance, NULL);
glfwDestroyWindow(window);
glfwTerminate();
}
int main() {
initWindow();
initVulkan();
mainLoop();
cleanup();
return 0;
}
You are most probably passing uninitialized values to your instance creation. This part of the code:
VkInstanceCreateInfo createInfo;
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
createInfo.pApplicationInfo = &appInfo;
Only initializes the sType and pApplicationInfo members of the create info, while other important members such as pNext are uninitialized and as such may contain values that the driver doesn't know how to handle, resulting in the segfault.
So as a general rule for all (Vulkan) structs: Zero initialize like this:
VkInstanceCreateInfo createInfo = {0};

How to get top-level windows and their names using libxcb

I have a Linux desktop with 2 open windows: a terminal and a browser. I'm trying to get the name of those windows with libxcb. Here's my code based on examples I found at https://www.systutorials.com/docs/linux/man/3-xcb_query_tree_reply/
Here's my code:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#include <xcb/xcb.h>
void get_children(xcb_connection_t* c, xcb_window_t window, xcb_window_t** children, int* count)
{
*count = 0;
*children = NULL;
auto cookie = xcb_query_tree(c, window);
auto reply = xcb_query_tree_reply(c, cookie, NULL);
if (reply)
{
*count = xcb_query_tree_children_length(reply);
*children = xcb_query_tree_children(reply);
free(reply);
}
}
void get_name(xcb_connection_t* c, xcb_window_t window, char** name, int* length)
{
*length = 0;
*name = NULL;
auto cookie = xcb_get_property(c, 0, window, XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 0, 0);
auto reply = xcb_get_property_reply(c, cookie, NULL);
if (reply)
{
*length = xcb_get_property_value_length(reply);
*name = (char*)xcb_get_property_value(reply);
free(reply);
}
}
int main()
{
auto c = xcb_connect(":0.0", NULL);
auto screen = xcb_setup_roots_iterator(xcb_get_setup(c)).data;
auto rootWindow = screen->root;
int nChildren;
xcb_window_t* children;
get_children(c, screen->root, &children, &nChildren);
for (int i = 0; i < nChildren; i++)
{
auto wid = children[i];
int length;
char* name;
get_name(c, wid, &name, &length);
printf("%u %d\n", wid, length);
}
return 0;
}
This returns 40 windows all with their name's length of 0. For example:
20971989 0
20971802 0
20972112 0
20972308 0
... (truncated for brevity)
I'm trying to get something like the output of wmctrl -l.
What am I doing wrong?
I figured out the problem. I needed to add a length to the xcb_get_property function call. The following code works.
auto cookie = xcb_get_property(c, 0, window, XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 0, 1000);

IMG_Load: Couldn't open xxx.png

Context: I am currently trying to practice my C skills a little bit with the SDL 2.0.7 and SDL2_image-2.0.2.
Problem: I get an error message during the execution of my program "IMG_Load: Couldn't open xxx.png". The error seems stupid as it is very explicit: "i can't find the image", but as the image is in the appropriate folder... I think I need a fresh eye to spot the stupid mistake.
Platform: Windows 10
IDE: Visual Studio 2017
Steps done to solve the problem:
1) Tried to reduce my code lenght/functionalities to its minimum. Result: Error is still here.
2) I created a new project and copy/pasted the simplified code. Result: On the new project, there is no error, everything is working fine.
3) I compared project's options and the folder. To me they are the same:
It shouldn't be useful but just in case, here is my:
Code sample:
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_render.h>
#include "SDL_timer.h"
int main(int argc, char *argv[])
{
printf("argc = %d\n", argc);
for (int i = 0; i < argc; ++i)
{
printf("argv[ %d ] = %s\n", i, argv[i]);
}
SDL_Window* pWindow = NULL;
SDL_Renderer* pRenderer = NULL;
SDL_Texture* pTexture = NULL;
SDL_Surface* pLoadedSurface = NULL;
SDL_Rect* tileClipsArray = NULL;
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER))
{
fprintf(stderr, "Erreur d'initialisation de la SDL : %s\n", SDL_GetError());
}
//Initialize PNG loading
int imgFlags = IMG_INIT_PNG;
if (!(IMG_Init(imgFlags) & imgFlags))
{
printf("IMG_Load: %s\n", IMG_GetError());
}
pWindow = SDL_CreateWindow("TestLoadingImage",
SDL_WINDOWPOS_CENTERED, // initial X position.
SDL_WINDOWPOS_CENTERED, // Initial Y position.
640, // Width, in pixels.
480, // Height, in pixels.
SDL_WINDOW_OPENGL); // Window flags
assert(NULL != pWindow);
//Create renderer for the window
pRenderer = SDL_CreateRenderer(pWindow,
-1, // Index of the rendering driver to initialize, -1 to initialize the first one supporting the requested flags.
SDL_RENDERER_ACCELERATED
| SDL_RENDERER_PRESENTVSYNC); // RendererFlags
assert(NULL != pRenderer);
//Initialize renderer color
SDL_SetRenderDrawColor(pRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
pLoadedSurface = IMG_Load("GroundTiles.png");
if (NULL == pLoadedSurface)
{
printf("IMG_Load: %s\n", IMG_GetError());
assert(NULL != pLoadedSurface);
}
//Create texture from surface pixels
pTexture = SDL_CreateTextureFromSurface(pRenderer, pLoadedSurface);
assert(NULL != pTexture);
//Get image dimensions
const int textureWidth = pLoadedSurface->w;
const int textureHeight = pLoadedSurface->h;
const int tileClipWidth = 128;
const int tileClipHeight = 128;
const int nbLines = textureHeight / tileClipHeight;
const int nbColumns = textureWidth / tileClipWidth;
const int nbTileClips = nbLines + nbColumns;
tileClipsArray = malloc(nbTileClips * sizeof(SDL_Rect));
int tileClipIndex = 0;
for (int tileClipLineIndex = 0; tileClipLineIndex < nbLines; ++tileClipLineIndex)
{
for (int tileClipColumnIndex = 0; tileClipColumnIndex < nbColumns; ++tileClipColumnIndex)
{
tileClipsArray[tileClipIndex].x = tileClipColumnIndex * tileClipWidth;
tileClipsArray[tileClipIndex].y = tileClipLineIndex * tileClipHeight;
tileClipsArray[tileClipIndex].w = tileClipWidth;
tileClipsArray[tileClipIndex].h = tileClipHeight;
++tileClipIndex;
}
}
//Get rid of old loaded surface
SDL_FreeSurface(pLoadedSurface);
pLoadedSurface = NULL;
int canLoop = 1;
SDL_Event event;
int lastUpdate = SDL_GetTicks();
int now = 0;
int timeToSpendPerClip = 5000;
int timeSpentwithThisClip = 0;
int clipToUse = 0;
while (canLoop)
{
now = SDL_GetTicks();
if (now - lastUpdate > 16)
{
timeSpentwithThisClip += now - lastUpdate;
lastUpdate = now;
// We are processing all the events received this frame.
while (SDL_PollEvent(&event))
{
// We need to know what kind of event we are dealing with.
switch (event.type)
{
case SDL_QUIT:
canLoop = 0;
break;
}
}
SDL_RenderClear(pRenderer);
if (timeSpentwithThisClip > timeToSpendPerClip)
{
clipToUse = rand() % 4;
timeSpentwithThisClip = 0;
}
// Set rendering space and render to screen.
SDL_Rect renderQuad;
renderQuad.x = 50;
renderQuad.y = 50;
renderQuad.w = tileClipsArray[clipToUse].w;
renderQuad.h = tileClipsArray[clipToUse].h;
SDL_RenderCopyEx(pRenderer, pTexture, &tileClipsArray[clipToUse], &renderQuad, 0.0, NULL, SDL_FLIP_NONE);
SDL_RenderPresent(pRenderer);
}
}
SDL_DestroyTexture(pTexture);
free(tileClipsArray);
SDL_DestroyRenderer(pRenderer);
pRenderer = NULL;
SDL_DestroyWindow(pWindow);
pWindow = NULL;
IMG_Quit();
SDL_Quit();
return EXIT_SUCCESS;
}
I'm probably going to copy/paste all my files from the project 1 into the project 2, but I would like to understand my mistake!

Segmentation fault initializing SDL2. What am I doing wrong with memory?

This is just suppose to display a bmp image to the SDL window front buffer. I played around with the code. And I think there is something wrong with my init() function. I'm new to SDL. But there must be a problem with my pointers or something I'm missing about SDL's fucntions
EDIT: I used GDB and it turned out my close() function was the problem. I believe it was because I was freeing memory that was set to NULL? I got rid of the close fucntion and just freed mem after my delay function.
#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdbool.h>
#define SCREENWIDTH 640
#define SCREENHEIGHT 480
SDL_Window *win = NULL;
SDL_Surface *scrn = NULL;
SDL_Surface *mscrn = NULL;
bool init()
{
bool suc = true;
char name[11] = "Hello SDL";
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf("%s", SDL_GetError());
suc = false;
}
win = SDL_CreateWindow(name, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREENWIDTH, SCREENHEIGHT, SDL_WINDOW_SHOWN);
if (win == NULL) {
printf("%s", SDL_GetError());
suc = false;
}
scrn = SDL_GetWindowSurface(win);
return suc;
}
bool loadmedia()
{
bool suc = true;
mscrn = SDL_LoadBMP("hello_world.bmp");
if (mscrn == NULL) {
printf("%s", SDL_GetError());
suc = false;
}
return suc;
}
void close()
{
SDL_FreeSurface(mscrn);
SDL_DestroyWindow(win);
SDL_Quit();
}
int main(int argc, char* args[])
{
if (!init()) {
close();
return 1;
}
if (!loadmedia()) {
close();
return 1;
}
SDL_BlitSurface(mscrn, NULL, scrn, NULL);
SDL_UpdateWindowSurface(win);
SDL_Delay(3000);
close();
return 0;
}
You should find a reasonable debugger and other tools to to find out which line is causing the error and why. Basically it boils down to using a debugger which usually comes with your IDE if you're using one, or using the very good code analysis tool, Valgrind.
If you're using gcc you can likely use gdb to debug your program easily. Here are some resources on how to help you diagnose segmentation faults:
Determine the line of C code that causes a segmentation fault?
http://www.cprogramming.com/debugging/segfaults.html
Get familiar with these tools, as they will save you countless hours in the future when you face new problems.

Color key transparency does not work with SDL_image and PNG files

I am trying to achieve transparency in SDL using color keying. While it does work with BMP files, it doesn't with PNG files.
Here is my code:
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface *displaySurface = SDL_SetVideoMode(200, 100, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
SDL_Surface *tmpSurface = NULL;
SDL_Surface *backgroundSurface = NULL;
SDL_Surface *bmpSurface = NULL;
SDL_Surface *pngSurface = NULL;
tmpSurface = IMG_Load("background.png");
backgroundSurface = SDL_DisplayFormat(tmpSurface);
SDL_FreeSurface(tmpSurface);
tmpSurface = SDL_LoadBMP("bmpImage.bmp");
bmpSurface = SDL_DisplayFormat(tmpSurface);
SDL_FreeSurface(tmpSurface);
SDL_SetColorKey(bmpSurface, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(bmpSurface->format, 255, 0, 255));
tmpSurface = IMG_Load("pngImage.png");
pngSurface = SDL_DisplayFormat(tmpSurface);
SDL_FreeSurface(tmpSurface);
SDL_SetColorKey(pngSurface, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(pngSurface->format, 255, 0, 255));
SDL_Rect dest;
dest.x = 0;
dest.y = 0;
SDL_BlitSurface(backgroundSurface, NULL, displaySurface, &dest);
SDL_BlitSurface(bmpSurface, NULL, displaySurface, &dest);
dest.x = 50;
SDL_BlitSurface(pngSurface, NULL, displaySurface, &dest);
SDL_Flip(displaySurface);
SDL_Event event;
while (1) {
while (SDL_PollEvent(&event)) {
;
}
SDL_Delay(1);
}
return 0;
}
I have uploaded both the code and the images I am using here:
http://tobias.braun-abstatt.de/files/forums/transparency_test.zip
If you are using SDL_image version 1.2.10 or 1.2.11, make sure to update it to version 1.2.12.
#include <SDL/SDL_image.h>
#if (SDL_IMAGE_MAJOR_VERSION != 1) || (SDL_IMAGE_MINOR_VERSION != 2) || (SDL_IMAGE_PATCHLEVEL < 12)
#error "Invalid SDL_image version"
#endif

Resources