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

Author Topic: Stack around the variable was corrupted  (Read 3515 times)

0 Members and 1 Guest are viewing this topic.

cowchin12

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Stack around the variable was corrupted
« on: February 17, 2013, 03:40:53 pm »
I can't get rid of this error I'm having so I was hoping someone could help. The error is "Stack around the variable 'player' is corrupted". This is the player variable that I created in Movement's constructor:

    sf::RectangleShape player(sf::Vector2f(40.f, 40.f));
    player.setOrigin(20.f, 20.f);
    player.setPosition(400.f, 40.f);
    player.setFillColor(sf::Color(10, 30, 180));

That does not cause the error, the error comes up when I make the movement object in main, like this:

Movement MovementObj1;

So can anyone help?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Stack around the variable was corrupted
« Reply #1 on: February 17, 2013, 03:49:31 pm »
Please provide a minimal and complete example that reproduces the error. Errors like this often occur somewhere else than the debugger will halt at.

Also when you're posting code, please make use of the code=cpp tag.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

cowchin12

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Stack around the variable was corrupted
« Reply #2 on: February 17, 2013, 03:58:46 pm »
Ok this is the movement constructor

#include "Globals.h"
#include "EnemiesPlayer.h"
#include "Levels.h"
#include "LevelObjects.h"
#include "Movement.h"
#include <SFML/Graphics.hpp>
#include <iostream>
#include <SFML/Audio.hpp>

Movement::Movement()
{
        sf::RectangleShape player(sf::Vector2f(40.f, 40.f));
        player.setOrigin(20.f, 20.f);
    player.setPosition(400.f, 40.f);
    player.setFillColor(sf::Color(10, 30, 180));

        float gravity = 980;
}
 


And this is main
#include "Globals.h"
#include "EnemiesPlayer.h"
#include "Levels.h"
#include "LevelObjects.h"
#include "Movement.h"
#include <SFML/Graphics.hpp>
#include <iostream>
#include <SFML/Audio.hpp>

int main()
{
        Movement MovementObj;
        //Movement *MovementObj2 = new Movement;

        sf::RenderWindow window(sf::VideoMode(800,600,32),"Winston the Fancy Dinosaur");

    // Limit frame-rate
    window.setFramerateLimit(60);

    // Keep track of the frametime
    sf::Clock frametime;

    // Big floor
    sf::RectangleShape floor(sf::Vector2f(800.f, 40.f));
    floor.setPosition(0.f, 560.f);
    floor.setFillColor(sf::Color(10, 180, 30));

    // Small box
    sf::RectangleShape box(sf::Vector2f(40.f, 40.f));
    box.setPosition(500.f, 480.f);
    box.setFillColor(sf::Color(10, 180, 30));

    while(window.isOpen())
    {
        float dt = frametime.restart().asSeconds();

        // Event handling
        sf::Event event;
        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
                        {
                window.close();
                }
                }

        // Render
        window.clear();
        //window.draw(MovementObj.player);
        window.draw(box);
        window.draw(floor);
        window.display();
    }
}
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Stack around the variable was corrupted
« Reply #3 on: February 17, 2013, 04:14:20 pm »
It's neither a complete nor a minimal example.
The error probably still is somewhere else. E.g. if you'd have an array of size 3, which then has the indices from 0-2 and then write to array[3], you'll corrupt the stack.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

cowchin12

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Stack around the variable was corrupted
« Reply #4 on: February 17, 2013, 04:24:55 pm »
O_o
This is going to be my entire program then...

Movement.h
#include <SFML/Graphics.hpp>
#include <iostream>
#include <SFML/Audio.hpp>

#pragma once
class Movement
{
public:
        Movement(void);
        sf::RectangleShape player;
        ~Movement(void);
       
private:
        float gravity;
};
 

Movement.cpp
#include "Movement.h"
#include <SFML/Graphics.hpp>
#include <iostream>
#include <SFML/Audio.hpp>

Movement::Movement()
{
        sf::RectangleShape player(sf::Vector2f(40.f, 40.f));
        player.setOrigin(20.f, 20.f);
        player.setPosition(400.f, 40.f);
        player.setFillColor(sf::Color(10, 30, 180));

        float gravity = 980;
}
 

main.cpp
#include "Movement.h"
#include <SFML/Graphics.hpp>
#include <iostream>
#include <SFML/Audio.hpp>

int main()
{
        Movement MovementObj;
        //Movement *MovementObj2 = new Movement;

        sf::RenderWindow window(sf::VideoMode(800,600,32),"Winston the Fancy Dinosaur");

    // Limit frame-rate
    window.setFramerateLimit(60);

    // Keep track of the frametime
    sf::Clock frametime;

    // Big floor
    sf::RectangleShape floor(sf::Vector2f(800.f, 40.f));
    floor.setPosition(0.f, 560.f);
    floor.setFillColor(sf::Color(10, 180, 30));

    // Small box
    sf::RectangleShape box(sf::Vector2f(40.f, 40.f));
    box.setPosition(500.f, 480.f);
    box.setFillColor(sf::Color(10, 180, 30));

    while(window.isOpen())
    {
        // Get delta time for frame-rate depended movement
        float dt = frametime.restart().asSeconds();

        // Event handling
        sf::Event event;
        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
                        {
                window.close();
                }
                }

        // Render
        window.clear();
                //window.draw(MovementObj.player);
        window.draw(box);
        window.draw(floor);
        window.display();
    }
}
 

That is all my program is now, still get the error

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Stack around the variable was corrupted
« Reply #5 on: February 17, 2013, 05:50:17 pm »
Why do you create a new local sf::RectangleShape named player in the Movement contructor that you never use? You probably wanted to use the member variable named player, since it's the one you want to draw.

cowchin12

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Stack around the variable was corrupted
« Reply #6 on: February 17, 2013, 06:34:13 pm »
Why do you create a new local sf::RectangleShape named player in the Movement contructor that you never use? You probably wanted to use the member variable named player, since it's the one you want to draw.

Ok thanks, i'm pretty bad at this stuff lol

 

anything