Memory not freeing when using free BUT continues to increase when using malloc

85 Views Asked by At

I've been trying to find information on this website about this, but the posts I've found have been a bit vague.

The program below uses SDL2 and SDL_image to display a spaceship. When the spacebar is pressed, it shoots a png out and the image moves to the right. Everything on screen is an object struct (stored in a linked list), and whenever the create_ent function is called, memory is allocated for the new object and it is displayed on screen. The move_ent function moves all objects on screen according to their xvector and yvector attributes. However, it also runs a check for whether or not the object is on screen. When the object is not on screen, it is SUPPOSED to free the object and perform the necessary (linked list) procedure for freeing the objects.

I've been using task manager as my memory usage analysis tool, and I've found that whenever the move_ents function is run, the memory is not freed. At least that is what Task Manager is telling me. I've seen in some posts that THE MEMORY IS KEPT FOR THE CURRENT PROGRAM AND IS NOT RELEASED TO THE OS (Memory usage isn't decreasing when using free?). I'm not completely sure why that is, but my memory usage is still going up every time it runs create_ent. If this is true, should I be using realloc instead?

Some PARTS of my code are below. I've included the move_ents and create_ent function definitions, their uses in main, as well as the object struct definition.

I'd also like to point out that Visual Studio is not giving me any errors.

Is this some weird thing with Task Manager? Or is there something wrong with my freeing of the memory?

#include <stdio.h>
#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_image.h>

#define WINDOW_WIDTH 1280
#define WINDOW_HEIGHT 720
#define TARGET_FPS 60

struct object {

    SDL_Surface* surface;
    SDL_Texture* texture;
    SDL_Rect rect;
    SDL_Point center;
    int is_ship;
    int inside;
    long float x;
    long float y;
    float w;
    float h;
    long float xvector;
    long float yvector;
    long float mass;
    long float forcex;
    long float forcey;
    float rot_speed;
    double angle;
    struct object* next;

};

void create_ent(SDL_Renderer* renderer, struct object* p, char* img_path, long float x, long float y, float w, float h, long float xvector, long float yvector, long float mass, double angle, int ship) {

    struct object* new = malloc(sizeof(struct object));

    while (p->next != NULL)
        p = p->next;

    p->next = new;

    new->surface = IMG_Load(img_path);
    if (!(new->surface))
        printf("Dammit! An image wasn't found.");

    new->texture = SDL_CreateTextureFromSurface(renderer, new->surface);

    new->x = x;
    new->y = y;
    new->w = w;
    new->h = h;
    new->xvector = xvector;
    new->yvector = yvector;
    new->mass = mass;
    new->forcex = 0;
    new->forcey = 0;
    new->inside = 1;
    new->angle = angle;
    new->center = (SDL_Point){ w / 2, h / 2 };
    new->is_ship = ship;
    new->next = NULL;

}

void move_ents(struct object* p) {

    struct object* temp;

    while (p->next != NULL) {

        if ((p->next)->inside == 0) {
            if ((p->next)->next == NULL) {
                free(p->next);
                p->next = NULL;
                break;
            }
            else {
                temp = (p->next)->next;
                free(p->next);
                p->next = temp;
            }
        }

        p = p->next;

        if (!(p->is_ship))
            if ((p->x > WINDOW_WIDTH + p->w / 2) || (p->y > WINDOW_HEIGHT + p->h / 2) || (p->x < 0 - p->w / 2) || (p->y < 0 - p->h / 2)) {
                p->inside = 0;
                printf("object outside of window");
            }

        p->xvector += (p->forcex / p->mass);
        p->yvector += (p->forcey / p->mass);
        p->x += p->xvector;
        p->y += p->yvector;

    }

}
 

int main(void) {
        while (SDL_PollEvent(&event) != 0) {

            switch (event.type) {
            case SDL_QUIT:
                quit = 1;
                break;

            case SDL_KEYUP:

                switch (event.key.keysym.sym) {
                case SDLK_SPACE:
                    create_ent(renderer, head, "allmine/Laser/laser.png", ship -> x + 10, ship -> y + 10, 5, 25, 10, 0, 1, ship -> angle, 0);
                    break;
                case SDLK_RIGHT:
                    rightpressed = 0;
                    break;
                case SDLK_LEFT:
                    leftpressed = 0;
                    break;
                case SDLK_UP:
                    uppressed = 0;
                    break;
                default:
                    break;
                }

                break;

            case SDL_KEYDOWN:

                switch (event.key.keysym.sym) {
                case SDLK_RIGHT:
                    rightpressed = 1;
                    break;
                case SDLK_LEFT:
                    leftpressed = 1;
                    break;
                case SDLK_UP:
                    uppressed = 1;
                    break;
                default:
                    break;
                }

            default:
                break;
            }

        }

        if (rightpressed)
            ship->angle += 5;
        else if (leftpressed)
            ship->angle -= 5;



        draw_ents(renderer, head);
        apply_forces(head, 10000);
        move_ents(head);

        SDL_RenderPresent(renderer);

        // Tickrate
        frameStart = SDL_GetTicks();
        frameTime = SDL_GetTicks() - frameStart;
        if (frameTime < 1000 / TARGET_FPS)
            SDL_Delay((1000 / TARGET_FPS) - frameTime);

    }
}
0

There are 0 best solutions below