Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Malloc Error  (Read 3721 times)

0 Members and 1 Guest are viewing this topic.

davejc

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Malloc Error
« on: October 05, 2018, 02:03:42 am »
I'm having a weird problem with my program that I don't understand at all. Whenever I close the program I get this message in the output:

malloc: *** error for object 0x100c7ef68: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

I put in the breakpoint, but don't know how to read it really. When I run the program and it gets to that breakpoint, there doesn't seem to be anything wrong. But when I continue I get to a line in the "new" system file at the
__builtin_operator_delete(__ptr);
line. It says
Thread 1: signal SIGABRT
. I don't know how to begin to analyze this, has anyone every had this same issue?

davejc

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: Malloc Error
« Reply #1 on: October 05, 2018, 02:27:18 am »
I found the source of the error, it happens when I try to draw some text to the screen. Don't know why this causes the error, but I'll keep digging

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Malloc Error
« Reply #2 on: October 05, 2018, 03:34:21 pm »
Some quick questions that may help someone understand why your code might be having this problem:
  • Are you using malloc?
  • Are you using threads?
  • Is the sf::Text or sf::Font in global scope?

Posting a complete and minimal example that still has this problem would also help greatly.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

davejc

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: Malloc Error
« Reply #3 on: October 06, 2018, 02:22:00 am »
No to all. The sf::Font is a member of the class. The sf::Text is a local variable in the class method. Here is a simple version of the method that is causing the error:

void Game::_pauseGame() {
        sf::Text pause, resume, options;

        // Each of these cause the error
        pause.setFont(_font);
        resume.setFont(_font);
        options.setFont(_font);
       
        // Set string of the text variables
        ....

        // Set the size and positioning of the text variables
        ....

        // Trying to draw each of the text variables causes the error as well
        _window.draw(pause);
        _window.draw(resume);
        _window.draw(options);
        _window.draw(_pauseSword);
}

I also get the error when exiting the application, but I think that has something to do with the destruction of either the sf::Text or sf::Font variables.

Also, I didn't mention in my first post, but I'm using Xcode. It works fine in VS

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Malloc Error
« Reply #4 on: October 15, 2018, 09:08:16 pm »
No to all. The sf::Font is a member of the class. The sf::Text is a local variable in the class method.
Which class? The Game class? Is that class instanced in global scope?

If no to this, the above mentioned minimal and complete example will probably be required.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

davejc

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: Malloc Error
« Reply #5 on: October 21, 2018, 11:56:17 pm »
In main, a Game object is instanced

Game myGame;

The Game's constructor initializes all Game components and enters the main run loop

void Game::_init() {
        // Initialize Media
        _initializeMedia();

        // Initialize System
        _initializeSystem();   

        // Run the game
        _run();
}

void Game::_run() {
        sf::Clock frameClock;

    while (_window.isOpen()) {
                // Update animation timing
                sf::Time frameTime = frameClock.restart();

                _updateWindow();
                _updateView();
       
                if (_state == PLAY)
                        _updatePlayers(frameTime);
       
        _draw();
    }
}

There is a method within the main loop that runs the pause logic which prints the pause menu and other things (as shown in the code in my previous post). This is the method that is causing my issues

davejc

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: Malloc Error
« Reply #6 on: December 16, 2018, 07:29:28 am »
Didn't find a solution and walked away for a while. Just got back to it and still am having the same problem. It's clear that the error is coming from the sf::Font/sf::Text. Apparently this issue was relatively common in older versions of Xcode and was fixed with an update to SFML. I am using Xcode 10.1 so that can't be the problem. And I am quite certain the version of SFML I'm using is stable.

I made the Game's instances of sf::Text/sf::Font members of the class and now the error occurs after the return statement in main. So again, clearly that is the issue. Just don't know how to solve it

davejc

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: Malloc Error
« Reply #7 on: December 16, 2018, 08:16:04 am »
Well of course, it's now working with a new download of SFML. Surprise surprise