LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-05-2014, 01:29 PM   #1
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 18.04 / CentOS 7.6
Posts: 397

Rep: Reputation: 62
Seg-faulting on my simple C sprite type.


I'm trying to use typedefs and structs in my simple C program. It compiles but it keeps segfaulting when run. It's using Allegro 5 (the al_* functions to load and release bitmaps) but all you really need to know is that I'm making a simple sprite type with x & y co-ordinates and a bitmap for the sprite itself. There is no draw function yet - it just sets things up and shuts it down.

sprite.h
Code:
#ifndef _sprite_h
#define _sprite_h

#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>

typedef struct {
        double x;
        double y;
        ALLEGRO_BITMAP *bitmap;
        int size_w;
        int size_h;
} SPRITE;

SPRITE* init_sprite(const char *filepath);
void destroy_sprite(SPRITE *s);

#endif
sprite.c
Code:
#define _sprite_c

#include <stdio.h>
#include "sprite.h"

SPRITE* init_sprite(const char *filepath) {
        SPRITE *s = malloc(sizeof(s));
        s->x = 0;
        s->y = 0;
        s->bitmap = al_load_bitmap(filepath);
        if (s->bitmap == NULL) {
                free(s);
                return NULL;
        } else {
                return s;
        }
}

void destroy_sprite(SPRITE *s) {
        if (s != NULL) {
                al_destroy_bitmap(s->bitmap);
                s->bitmap = NULL;
                free(s);
        }
}

#endif
I'm using it in my main() with: -

Code:
#include "sprite.h"
SPRITE *sprite;
sprite = init_sprite("sprite.png");
// Enter main loop and do stuff...
// Then shutdown.
destroy_sprite(sprite);
I'm clearly doing something dreadfully wrong but I don't see what. Can somebody help me out please?

Last edited by arashi256; 04-05-2014 at 03:08 PM.
 
Old 04-05-2014, 01:38 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
As a start, use gdb to decide where the program stops.

Code:
gdb ./myexe
run
...
backtrace
 
Old 04-05-2014, 03:14 PM   #3
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 18.04 / CentOS 7.6
Posts: 397

Original Poster
Rep: Reputation: 62
Okay, as I added some printf() statements to see where it was crashed out and it appears to be when I'm calling destroy_sprite()...I think. Because that's the only call to free() after the game loop finishes.


Code:
*** glibc detected *** ./gameloop: free(): invalid next size (fast): 0x088f9530 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0xb74beee2]
./gameloop[0x804921e]
./gameloop[0x8049eda]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb74624d3]
./gameloop[0x8049111]
Is my destroy_sprite() code wrong? What does "free(): invalid next size" mean in this context?

EDIT: I've definitely narrowed it down to the "free(s);" call in destroy_sprite(). If I take out that, I still get a segfault/core dump but I don't see what I'm doing wrong. Every call to malloc() needs a corresponding free(). So if I leave it in, I get the backtrace above. If I take it out (memory leak) I get a core dump.

Last edited by arashi256; 04-05-2014 at 03:30 PM.
 
Old 04-05-2014, 03:56 PM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
This statement is wrong:
Code:
SPRITE *s = malloc(sizeof(s));
It should be:
Code:
SPRITE *s = malloc(sizeof(SPRITE));
Because of the error, it is highly probably that in the course of your application, you smashed the heap with errant data writes to the SPRITE pointer.

Note: The sizeof(s) returns the size of the pointer (4 bytes on a 32-bit system, or 8 bytes on a 64-bit system).

Last edited by dwhitney67; 04-05-2014 at 03:58 PM.
 
1 members found this post helpful.
Old 04-05-2014, 04:08 PM   #5
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 18.04 / CentOS 7.6
Posts: 397

Original Poster
Rep: Reputation: 62
That's fixed it! Thank you - I can't believe I didn't spot that. Ugh.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache + mod_ldap seg faulting - what am I doing wrong? patniemeyer Fedora 3 03-05-2009 04:12 PM
mv seg faulting? Tweakin Linux - General 1 05-05-2006 12:05 PM
Seg Faulting C. Where? DaveyB Programming 9 11-04-2004 02:00 PM
Compiler seg faulting fitz9948 Slackware 2 05-11-2004 03:04 PM
My program is seg faulting? eggs Programming 2 10-06-2002 02:32 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration