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 8667611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:00:31+00:00 2026-06-12T18:00:31+00:00

I have a class Block which calls SDL_FreeSurface(surface) in the destructor. In the main()

  • 0

I have a class Block which calls SDL_FreeSurface(surface) in the destructor. In the main() when I create an instance of Block, the object functions properly but when I used it in another class Control ,which has vector<Block> block_vector as a data member, the program crashes when I add an instance of Block into the block_vector. I narrowed down the problem which is the destructor of the Block when calling SDL_FreeSurface(surface). Does adding objects to the vector have anything to do with is? What is the problem?

 class Block{

public:

    Block(int x, int y);
   ~Block();


    void Load_Image(MediaFunctions &M_Functions);

    void SetPosition(int x, int y);

    void BlitBlock(SDL_Event &event, MediaFunctions &M_Functions, SDL_Surface *destination);


    bool DetectionNames(SDL_Event &event, MediaFunctions &M_Functions, SDL_Surface *destination);

    bool DetectionHours(SDL_Event &event, MediaFunctions &M_Functions, SDL_Surface *destination);

    bool return_error();



private:


    SDL_Surface *block_surface_names;
    SDL_Surface *block_surface_hours;

    SDL_Surface *block_names_detected;
    SDL_Surface *block_hours_detected;

    SDL_Rect     block_rect_names;
    SDL_Rect     block_rect_hours;


    bool error;

    };


    //the problem
    Block::~Block(){


    SDL_FreeSurface(block_surface_hours);
    SDL_FreeSurface(block_surface_names);

    SDL_FreeSurface(block_hours_detected);
    SDL_FreeSurface(block_names_detected);

    }



    //when doing this 
    void Control::HandleEvents(SDL_Event &event, MediaFunctions &M_Functions){

if(event.type == SDL_KEYDOWN){

    if( event.key.keysym.sym == SDLK_a ){

            //append a block instance
            BlockVector.push_back(Block (Block(100,100)));
            BlockVector.at(block_index).Load_Image(M_Functions);

            block_index++;

    }

    }

    } 
  • 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-12T18:00:32+00:00Added an answer on June 12, 2026 at 6:00 pm

    When you insert one of your Block objects into the vector, a copy is made. Since you didn’t provide a copy constructor, the compiler generated copy constructor is used. The compiler generated copy constructor will simply copy your pointers over. It won’t copy the actual SDL surfaces.

    That means that your original Block object, and the Block object that is inserted into your vector both have pointers to the same SDL surfaces. When the lifetime of one of those Block objects ends, the destructor is called, which frees the SDL surface. Now the other Block object has a dangling pointer, and when its lifetime ends, and the destructor is called, you call SDL_FreeSurface on a dangling pointer.

    This is almost certainly what is causing your crash. That’s why you need to follow the Rule of Three, and properly implement both a copy constructor, and an assignment operator. The implementation is up to you. In most cases, I would recommend doing a full, deep copy of your objects. But for such heavyweight objects as SDL surfaces, I would recommend you look at implementing some kind of reference counting mechanism, or use std::shared_ptr with a custom deleter that calls SDL_FreeSurface instead of delete.

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

Sidebar

Related Questions

I have a class in which i have intialized hashmap in static block. Passing
I have a class hierarchy, each member of which may create IDisposable objects. I
Hi I have a class with multiple methods in which I require synchronized blocks
Hi I have following code block public class Driver { static String x =
I have the following block of code in my bean class - HttpServletResponse response
I have Class and Student objects. Both have collection of another as property. Which
I have class LegacyClass which inherits OldBaseClass. I'm considering a change to introduce a
we have a C++ class which basically reads and writes vectors from a binary
I have a method, which has a try/catch/finaly block inside. Within the try block,
This is a client part of programm. I have such class which works with

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.