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

Author Topic: sf::RenderWindow::Clear() causing Unhandled exception  (Read 15821 times)

0 Members and 1 Guest are viewing this topic.

fazeli

  • Newbie
  • *
  • Posts: 9
    • View Profile
sf::RenderWindow::Clear() causing Unhandled exception
« on: April 26, 2010, 10:44:06 pm »
(Complete beginner)

Trying to go through the tutorials, stuck on "Graphics - Using render windows" (http://sfml-dev.org/tutorials/1.6/graphics-window.php).  It seems the App.Clear() and App.Clear(sf::Color(200, 0, 0)) are both causing

First-chance exception at 0x00434155 in SFML_GraphicsWindow.exe: 0xC0000005: Access violation reading location 0x43960010.
Unhandled exception at 0x00434155 in SFML_GraphicsWindow.exe: 0xC0000005: Access violation reading location 0x43960010.

After commenting out the App.Clear sections, the code runs without exception.

I've tried going through the source with the debugger but as I mentioned I am a beginner.

Any suggestions would be greatly appreciated.  Thank you.

nulloid

  • Full Member
  • ***
  • Posts: 134
    • View Profile
sf::RenderWindow::Clear() causing Unhandled exception
« Reply #1 on: April 26, 2010, 11:13:13 pm »
Could you show the code?

fazeli

  • Newbie
  • *
  • Posts: 9
    • View Profile
sf::RenderWindow::Clear() causing Unhandled exception
« Reply #2 on: April 27, 2010, 12:17:49 am »
Sorry my bad.  The code that was being used is the same as the one in the tutorial in this file http://sfml-dev.org/tutorials/1.6/sources/graphics-window.cpp.  I have included a copy here below stripped of comments.

#include <SFML/Graphics.hpp>

int main() {
   sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");

   while (App.IsOpened()) {
      sf::Event Event;
      while (App.GetEvent(Event)) {
         if (Event.Type == sf::Event::Closed)
            App.Close();

         if (Event.Type == sf::Event::KeyPressed) {
            if (Event.Key.Code == sf::Key::Escape)
               App.Close();

            if (Event.Key.Code == sf::Key::F1) {
               sf::Image Screen = App.Capture();
               Screen.SaveToFile("screenshot.jpg");
            }
         }
      }

      App.Clear(sf::Color(200, 0, 0));

      App.Display();
   }

   return EXIT_SUCCESS;
}

Additional Information.
I've linked the following files: sfml-system-s.lib  sfml-graphics-s.lib sfml-window-s.lib

There are no additional .dll in the directory that the file is being executed in as I don't believe I need as I am using the static libraries.

Newly discovered.  Oddly enough when I link to sfml the static-debug versions of the libraries files, the code runs properly.  However, compiling results in many warnings similar to the following.

1>sfml-system-s-d.lib(Platform.obj) : warning LNK4099: PDB 'vc80.pdb' was not found with 'C:\SFML-1.6\lib\sfml-system-s-d.lib' or at 'c:\Documents and Settings\user\My Documents\Visual Studio 2005\Projects\SFML_GraphicsWindow\debug\vc80.pdb'; linking object as if no debug info

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
sf::RenderWindow::Clear() causing Unhandled exception
« Reply #3 on: April 27, 2010, 01:54:49 am »
What's the -s, did you rename your dlls?

-d is for debug and nothing there is the release ones.

Debug version:

sfml-graphics-d.dll

Release version:

sfml-graphics.dll


BTW, I would fix your indentation blocks because it's hard as hell to tell where your nests are.

fazeli

  • Newbie
  • *
  • Posts: 9
    • View Profile
sf::RenderWindow::Clear() causing Unhandled exception
« Reply #4 on: April 27, 2010, 01:59:26 am »
-s = static, which as I understand it means the dlls do not have to be copied to current executable directory nor do they need to be renamed.  those libs without the -s mean dynamic in which case you have to copy the dll to the current directory (my understanding maybe incorrect) but I believe I read it in http://www.sfml-dev.org/tutorials/1.6/start-vc.php

yea the paste that I did resulted in poorly readable code, but I figured it was short enough that there was no need to repaste.

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
sf::RenderWindow::Clear() causing Unhandled exception
« Reply #5 on: April 27, 2010, 02:21:45 am »
Oh that's right.

Maybe you need to set your project for release?

fazeli

  • Newbie
  • *
  • Posts: 9
    • View Profile
sf::RenderWindow::Clear() causing Unhandled exception
« Reply #6 on: April 27, 2010, 02:50:31 am »
Could you please elaborate on what you mean by "set your project for release"?  I'm very new to MSVS, so please overlook my ignorance.

nulloid

  • Full Member
  • ***
  • Posts: 134
    • View Profile
sf::RenderWindow::Clear() causing Unhandled exception
« Reply #7 on: April 27, 2010, 01:34:14 pm »
[OFF] You can use the Code and /Code tags ;)
Code: [Select]
// Like this

[ON]
Quote from: "fazeli"
Could you please elaborate on what you mean by "set your project for release"? I'm very new to MSVS, so please overlook my ignorance.


There should be a "release" and a "debug" configuration of your project. Make sure that you link against debug libraries in the debug config, and against release libs in the release config. See http://msdn.microsoft.com/en-us/library/wx0123s5.aspx

Another thing is the order of linking. It is mentioned somewhere in the SFML tuts that if A lib depends on B, you should link first A, then B. I don't know what is the effect of mixing the order, I have never tried it :) But maybe this is the cause. If not, I would check whether the creation of sf::RenderWindow was successful. Can't be sure :)

fazeli

  • Newbie
  • *
  • Posts: 9
    • View Profile
sf::RenderWindow::Clear() causing Unhandled exception
« Reply #8 on: April 27, 2010, 05:41:20 pm »
Code: [Select]
#include <SFML/Graphics.hpp>

int main() {

    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");

    while (App.IsOpened()) {
        sf::Event Event;
        while (App.GetEvent(Event)) {
            if (Event.Type == sf::Event::Closed)
                App.Close();
            if (Event.Type == sf::Event::KeyPressed) {
                if (Event.Key.Code == sf::Key::Escape)
                    App.Close();

                if (Event.Key.Code == sf::Key::F1) {
                    sf::Image Screen = App.Capture();
                    Screen.SaveToFile("screenshot.jpg");
                }
            }
        }

        App.Clear(sf::Color(200, 0, 0));
        App.Display();
    }

    return EXIT_SUCCESS;
}


Thank you for all the suggestions!  The code tags were most helpful.  I'll hunt down the statements about the order of the libs, but I had thought that the order likely wouldn't matter because of #ifdefs and#defines but I'll double check.

I had thought that sf::RenderWindow::isOpened() would check if the render window had been created successfully but I'll read about that in the documentation.  I was also confident that it was being created successfully (as well as opened and all that) because the "Unhandled Exception" problem did not occur when sf::RenderWindow::Clear() was commented out and the code executed as described.

nulloid

  • Full Member
  • ***
  • Posts: 134
    • View Profile
sf::RenderWindow::Clear() causing Unhandled exception
« Reply #9 on: April 28, 2010, 09:03:14 pm »
Quote from: "fazeli"
I was also confident that it was being created successfully (as well as opened and all that) because the "Unhandled Exception" problem did not occur when sf::RenderWindow::Clear() was commented out and the code executed as described.


Sorry, I tend to say really stupid things :)

Well... maybe a call stack would show the source of the problem.

fazeli

  • Newbie
  • *
  • Posts: 9
    • View Profile
sf::RenderWindow::Clear() causing Unhandled exception
« Reply #10 on: April 28, 2010, 11:12:57 pm »
I'm sure the code originally posted without proper formatting didn't help the matter.

I haven't leaned the proper use of the debugger yet but here's my Call Stack when the -s-d.lib were changed back to -s.lib

Code: [Select]

 (yellow arrow)   SFML_GraphicsWindow.exe!sf::RenderTarget::Clear()  + 0x5 bytes C++
 (green arrow)  > SFML_GraphicsWindow.exe!main()  Line 41 C++
SFML_GraphicsWindow.exe!__tmainCRTStartup()  Line 597 + 0x19 bytes C
                SFML_GraphicsWindow.exe!mainCRTStartup()  Line 414 C
                kernel32.dll!7c817077()
                [Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]

nulloid

  • Full Member
  • ***
  • Posts: 134
    • View Profile
sf::RenderWindow::Clear() causing Unhandled exception
« Reply #11 on: April 28, 2010, 11:38:59 pm »
Hmmm.. I don't know if anyone can see the problem from this, but I can't. Here is what I suggest:

- Switch back to debugging libraries.
- Start the program in debug mode (I assume you are using some kind of IDE, and that feature is available)
- Put a breakpoint just before the App.Clear(); statement
- now when the arrow points to that statement (indicating that this is the next statement to execute), use "step in"
- Somewhere it will segfault. Probably in a function, so remember that function, and start all over again this procedure, but step in that function.
- Always advance a single step at a time in the function where you don't know where is the problem. (Except of course if it is a loop which cycles 100000 times or things like that :))

If you do just as this, eventually you will find yourself at a single statement throwing segfault. At that point, copy the callstack, and the current line you are on.
OR
wait for some other responses :)

fazeli

  • Newbie
  • *
  • Posts: 9
    • View Profile
sf::RenderWindow::Clear() causing Unhandled exception
« Reply #12 on: April 28, 2010, 11:49:48 pm »
Sounds like a plan to me and has the added benefit that I don't have to read on how the MS VS 2005 debugger works!

fazeli

  • Newbie
  • *
  • Posts: 9
    • View Profile
sf::RenderWindow::Clear() causing Unhandled exception
« Reply #13 on: April 29, 2010, 12:27:20 am »
Didn't seem to add any information for me, although it could hit me in the face and I might still not know.  Here's the information that I received.

Message from application as it was running.

Quote
Unhandled exception at 0x0042f835 in SFML_GraphicsWindow.exe: 0xC0000005: Access violation reading location 0x43960010.


Code: [Select]

(circled yellow arrow) SFML_GraphicsWindow.exe!sf::RenderTarget::Clear()  + 0x5 bytes C++
(green arrow) SFML_GraphicsWindow.exe!main()  Line 41 C++
  SFML_GraphicsWindow.exe!__tmainCRTStartup()  Line 597 + 0x19 bytes C
  SFML_GraphicsWindow.exe!mainCRTStartup()  Line 414 C
  kernel32.dll!7c817077()
  [Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]


Disassembly window reads
Code: [Select]

// Clear the screen with red color
        App.Clear(sf::Color(0, 255, 0));
(red arrow) 0042E6B6  push        0FFh
0042E6BB  push        0    
0042E6BD  push        0FFh
0042E6C2  push        0    
0042E6C4  lea         ecx,[ebp-458h]
0042E6CA  call        sf::Color::Color (42B12Ch)
0042E6CF  push        eax  
0042E6D0  lea         ecx,[ebp-84h]
0042E6D6  call        sf::RenderTarget::Clear (42B2D5h)

        // Display window contents on screen
App.Display();
(green arrow) 0042E6DB  lea         ecx,[ebp-314h]
0042E6E1  call        sf::Window::Display (42B762h)


Thanks for the help thusfar but it's not too important for me right now if this is an error that can't be reproduced, as I am content with using the -s-d.libs as I learn.

nulloid

  • Full Member
  • ***
  • Posts: 134
    • View Profile
sf::RenderWindow::Clear() causing Unhandled exception
« Reply #14 on: April 29, 2010, 02:52:33 am »
I wrote a little program (finished tonight), and I used the graphics module in it. I'm in Ubuntu Karmic, using libmesa. And a lot of time, some radeon function throws a segfault right after I close the application (I believe this is a bug in libmesa). So maybe (just maybe) the problem is in your environment (driver, etc.), and not in SFML. I'm not enough here, I guess. Sorry :S

But if you don't want to hunt down memory corruptions, here is a couple things you could try:

-test if other Graphics-related things work well (drawing, moving a view, etc.)
-try other window sizes / resolutions
-update your driver
-try it on different compilers (who knows...)
-worst-case scenario: update to sfml2
(+1: reboot your computer :D)

Anyway, I think Laurent should read this topic, maybe he can ask the right questions :)