Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8759137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:43:32+00:00 2026-06-13T14:43:32+00:00

I’m trying to write a game using SDL, but i’m getting a lot of

  • 0

I’m trying to write a game using SDL, but i’m getting a lot of strange segfaults.

I have created class Monster and Player which get public variables from class Creature. Class Creature gets variables from class Object. Just like that:

class Object {
public:
    Area* area_pointer; // Pointer to an Area in which object is present.
    Sprite sprite; // Class that has SDL_Surface* and some other things.
    Animation animation; // nothing usefull
    int ID;
    short int position_x; // nothing usefull
    short int position_y; // nothing usefull
    short int effect_x; // nothing usefull
    short int effect_y; // nothing usefull
    .... //Some functions that are not important right now.
};

Class Sprite:

class Sprite {
public:
    SDL_Surface* image;
    short int x;
    short int y;
    short int w;
    short int h;
    Sprite ()
    {
        image = NULL;
        x = 0;
        y = 0;
        w = 0;
        h = 0;
    }   
};

Class Creature, Player, Monster:

class Creature : public Object {
        public:
            char move_direction;
            char speed; 
            ..// Some not important functions.
};
class Player : public Creature {
        public:
            char select_type;
            int select_ID;
            std::vector <Item> equip;
            std::vector <Item> inventory;
            std::vector <Effect> effects;
            Player (Area* c_area_pointer, int data [])
            {
                area_pointer = c_area_pointer;
                sprite.image = SurfaceLoad ("Images/Players/" + IntToString (data [0]) + ".png");
                sprite.x = 0;
                sprite.y = 0;
                sprite.w = 0;
                sprite.h = 0;
                ID = data [0];
                position_x = data [1];
                position_y = data [2];
                effect_x = 0;
                effect_y = 0;
            }
            ~Player ()
            {
                SDL_FreeSurface (sprite.image);
                .....
            }
};
class Monster : public Creature {
        public:
            char type;
            std::vector <Item> loot;
            std::vector <Effect> effects;
            Monster (Area* c_area_pointer, int data [])
            {
                area_pointer = c_area_pointer;
                sprite.image = SurfaceLoad ("Images/Monsters/" + IntToString (data [3]) + ".png");
                sprite.x = 0;
                sprite.y = 0;
                sprite.w = 0;
                sprite.h = 0;
                ID = data [0];
                position_x = data [1];
                position_y = data [2];
                effect_x = 0;
                effect_y = 0;
                type = data [3];
            }
            ~Monster ()
            {
                SDL_FreeSurface (sprite.image);
                .....
            }
};

SurfaceLoad function:

SDL_Surface* SurfaceLoad (std::string file_name)
{
SDL_Surface* surface_1 = NULL;
SDL_Surface* surface_2 = NULL;
surface_1 = IMG_Load (file_name.c_str ());
surface_2 = SDL_DisplayFormat (surface_1);
if (surface_1 != surface_2) SDL_FreeSurface (surface_1); // This line may be strange to some of you, but it is related to other error i had in past.
SDL_SetColorKey (surface_2, SDL_SRCCOLORKEY, 0xFFFFFF);
return surface_2;
}

I try to load data from txt files and make objects basing on it. I use my own Load function to that. It creates a pointer to the class object for example:

        Player* player;
        std::string players_name = save_name + "Areas/" + IntToString (ID) + "/players.txt"; //Path to the file cointaining players data.
        std::ifstream players_file;
        players_file.open (players_name.c_str ());
        Monster* monster;
        std::string monsters_name = save_name + "Areas/" + IntToString (ID) + "/monsters.txt"; //Path to the file cointaining monsters data.
        std::ifstream monsters_file;
        monsters_file.open (monsters_name.c_str ());

Then it loads data from text file and puts it into array of int named file_data and creates new class object based on it.

        while (!players_file.eof ())
        {
            getline (players_file, file_text);
            while (file_text [data_position_2 + 1] != ';')
            {
                data_position_2 = file_text.find (",", data_position);
                data.assign (file_text, data_position, data_position_2 - data_position);
                file_data [data_index] = atoi (data.c_str ());
                data_position = data_position_2 + 1;
                data_index++;
            }
            player = new Player (this, file_data);
            this->area_map.players.push_back (*player); //Vector players inside object area_map which contain also monsters vector. "this" is a pointer to object that contain Load function and area_map object.
            delete player;
            data_index = 0;
            data_position = 0;
            data_position_2 = 0;
        }

This part of the code works, but doing exactly the same thing with monsters_file and monsters vector caused a lot of strange errors. First error that i got was segfault when deleting a pointer after pushing data pointer by it to the vector. I checked it and found out that program crashes (segfault) in deconstructor when it calls SDL_FreeSurface (). So i checked if my constructor loads surface properly. I found out that everything is ok with constructing an object, but then it suddenly started to crash (segfault) when calling SurfaceLoad (). Checked this function too and everything was ok with that too: pointers to surfaces were ok, pointer returned by it was ok, but for some reason it crashed at:

sprite.image = SurfaceLoad (...);

Some time after it stopped to crash here, without any reason (I just added that line

if (surface_1 != surface_2) SDL_FreeSurface (surface_1);

, because i noticed that SDL_DisplayFormat () sometimes returns same pointer as pointer to the unformatted surface.) and started to crash (segfault too) when i push_back object pointed by pointer to the vector:

this->area_map.monsters.push_back (*monster);

Monster and Player classes are pretty much the same on this stage of creating the game, so i have no idea why it creates players without any problem and it has so many problems with creating monsters. Has anyone any idea how to fix that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:43:33+00:00Added an answer on June 13, 2026 at 2:43 pm

    Well loads of code which looks more than a little disorganized. For instance why is the Player destructor freeing the image which is declared in Sprite? Shouldn’t that be the job of the Sprite destructor? And why is everything public? But anyway from the symptoms you describe it sounds like a classic case of failing to follow the rule of three.

    When you write classes that allocate memory or other resources internally (like the image pointer in Sprite) then you must write copy constructors and assignment operators that handle the allocated memory or resource correctly. If you don’t you get errors like these.

    See here for some essential information on the rule of three. If you don’t understand this stuff then you’ll always be writing buggy C++ code. The section on managing resources is the most relevant to you, but read it all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.