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

Author Topic: Please help me understand sf::Event  (Read 1968 times)

0 Members and 3 Guests are viewing this topic.

Raptor88

  • Full Member
  • ***
  • Posts: 111
    • View Profile
    • Email
Please help me understand sf::Event
« on: July 27, 2012, 11:31:57 am »
I'm just learning C++ and SFML so please be gentle  :).

The code listed below are excerpts from a SFML game tutorial that's posted on youtube.
The "while ( App.IsOpened() )" statement appears to be the game loop.
Just below that is the statement:  "sf::Event event;"

MY QUESTION:
Isn't the statement "sf::Event event;" creating an instance of the "Event" class?
If that's true, what prevents thousands of instances of "event" being created due to the while loop, like a huge memory leak?

Thanks,
Raptor



// Some include statements here.

// Some initialization statements here.

WorldBase *TargetWorld;

sf::RenderWindow App;

int main(int argc, char** argv)
{
  App.Create(sf::VideoMode( .... too long to type....);

  TargetWorld = new LevelWorld();

  while ( App.IsOpened() )
  {
     sf::Event event;
     while (App.PollEvent(event) )
     {
        TargetWorld -> input (event);
        switch (event.Type)
        {
            case sf::Event::Closed:
               App.Close();
               break;

             case // some more code here not pertinent to my question.

             break;
         }
     }
  }
  return 0;
}





i514x

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Please help me understand sf::Event
« Reply #1 on: July 27, 2012, 11:34:55 am »
Dude, if a variable is created within the loop (in the brackets) after the next iteration, it is destroyed.

Do:
{ int Variable = 5; }
std::cout << Variable;
Will give you error because Variable will not exist.

Acrobat

  • Full Member
  • ***
  • Posts: 153
    • View Profile
Re: Please help me understand sf::Event
« Reply #2 on: July 27, 2012, 12:39:51 pm »
When you write something like :

sf::Event event;
 

compiler create a block with size equal to sizeof(sf::Event) and place it in the data section of you executable (or just on stack), when you try to access the variable,  compiler replace it with code, something like

mov eax, dword prt [address of structure + offset to variable]
 

Set a watch on event variable, go out of scope, in Watch you can see it filled with last handeled vars.
« Last Edit: July 27, 2012, 12:44:29 pm by Acrobat »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Please help me understand sf::Event
« Reply #3 on: July 27, 2012, 06:29:38 pm »
Dude, if a variable is created within the loop (in the brackets) after the next iteration, it is destroyed.
[...]
Will give you error because Variable will not exist.
Please make use of proper English sentences (if you can) and don't use 'dude', this is a forum and not a chat/real life conversation. ::)

When you write something like :

sf::Event event;
 

compiler create a block with size equal to sizeof(sf::Event) and place it in the data section of you executable (or just on stack), when you try to access the variable,  compiler replace it with code, something like

mov eax, dword prt [address of structure + offset to variable]
 

Set a watch on event variable, go out of scope, in Watch you can see it filled with last handeled vars.
I bet getting down to C and ASM won't help him understand anything... ;D

Raptor88 you should maybe take a closer look again at your C++ book, specially the memory managment part. ;)
sf::Event event; indeed creates an instance on the stack, but it will be freed automatically whenever you leave the scope (as i514x said the { }-section). Thus you won't get any memory leak. Since the event class is quite thin the overhead of creating and destroying doesn't really have to bother you.
If you now, for some strange reason, would use sf::Event* event = new sf::Event; the space will be allocated (= reserved on the 'heap') and if you'd do it every itereation and wouldn't call delete on it at the end of the main loop, you would create memory leaks non-stop. But as a side note, if you still would call delete at the end of the main loop, it wouldn't be guaranteed that your application doesn't produce memory leaks, beacause you could easily call return 0; or break; and exit the loop before you reach the delete statement. On top of that an exception could also completly jump over that delete statement. That's why smart pointers were introduces. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Raptor88

  • Full Member
  • ***
  • Posts: 111
    • View Profile
    • Email
Re: Please help me understand sf::Event
« Reply #4 on: July 27, 2012, 09:36:21 pm »
Hi i514x, Acrobat and eXpl0it3r,

Thanks a lot for your responses.

My problem was that I didn't realize that each pass through a while loop actually "exited" its scope { } brackets on every pass.  I always thought that a while loop remained inside of the { } brackets the whole time until the while loop was exited.  No book I ever read explained this crutial bit of information in the light of exiting or remaining within the { } brackets on every pass.

The example that i514x gave of outputting "Variable" to the console outside of the while's { } brackets was not quite what I didn't understand.  That part is explained in C++ books.  The actually exiting the while's { } brackets while still looping is what is not explained in any C++ book I've ever read.

Now I understand how it works and a big thanks to each and everyone of you,
Raptor
« Last Edit: July 27, 2012, 09:37:56 pm by Raptor88 »

 

anything