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

Author Topic: Closing window causes access violation  (Read 12254 times)

0 Members and 1 Guest are viewing this topic.

Beta_Ravener

  • Jr. Member
  • **
  • Posts: 51
    • ICQ Messenger - 271426715
    • View Profile
Closing window causes access violation
« on: October 20, 2011, 07:58:35 pm »
My 2 programs using SFML 2.0 have both some strange glitch, that doesn't affect them but it's annoying. I'm creating sf::Window and sf::Render window in first and sf::Window in second and both programs are using also console window from sfml-main. When those windows appers on screen they behave correct and everything, but closing them through X in right up corner cause the created windows disappear (I think they close correctly), but that console window stays stuck for 3-4 seconds and then it crash. Note that when I close window my programs end(because of loop that uses sf::Window::IsOpened()). I get access violation, but it's not in my code and gets thrown after my main() function returns and formally ends program. The strange thing is, that when program is closed by clicking X in corner of console window everything closes correctly and no error appears.

I'm still not sure if it's bug in my code or SFML, because I was unable to jump from my code to SFML files. It can be still that I forgot to do something with resource inside my code, but I checked it few times and made destructors for almost every class cleaning those resources before program ends. Didn't help though, so I deleted those destructors for not to make more mess than there already is.

And sorry if there's topic like this, I couldn't find anything helpful (other than some topic about SFML using globals bacause of some ATI driver bug.. so maybe it'd help to say that I'm using ATI).

Beta_Ravener

  • Jr. Member
  • **
  • Posts: 51
    • ICQ Messenger - 271426715
    • View Profile
Closing window causes access violation
« Reply #1 on: October 20, 2011, 11:25:24 pm »
Ok I got something.. though I still can't jump to sfml source to track it down, I just noticed that upon exit in debug mode I got this line in console: "An internal OpenGL call failed in Image.cpp (90) : GL_INVALID_OPERATION, the specified operation is not allowed in the current state".

Also in debug mode, visual studio can look into disassmbly and tell me some info. Well when my main function returns, it's Main thread[5092] and stack frame is Waves.exe!_tmainCRTStartup(and some more info), but it stays here for about 6 steps. Then it executes: "0141A10F  call        dword ptr [__imp__exit (1430B04h)] ". It gets through msvcr90d.dll, kernel32.dll, ntdll.dll, and maybe others.. it's probably cleaning this debug info. But what's interesting, it finally gets to that line causing troubles: "69092994  mov         edi,dword ptr [eax+8] " in Main Thread[5092] and stack frame is "atioglxx.dll!69092994()" .

Hope this helps somehow

sbroadfoot90

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Re: Closing window causes access violation
« Reply #2 on: October 21, 2011, 12:35:44 am »
Quote from: "Beta_Ravener"
I'm still not sure if it's bug in my code or SFML


care to show your code?

Beta_Ravener

  • Jr. Member
  • **
  • Posts: 51
    • ICQ Messenger - 271426715
    • View Profile
Re: Closing window causes access violation
« Reply #3 on: October 21, 2011, 02:02:32 pm »
Quote from: "sbroadfoot90"
care to show your code?


Sure.. in fact while trying to get together some code to post here, I've found exact place where this happens. It happens when I call single function in my first program with 2 windows (the second one is out because it had the very same error but it was because of one vector).

This is the code for rendering second window, which is something like child to first, because it closes when the first one closes.
Code: [Select]
void SettingsUI::Render(){
window.SetActive();
window.Clear(sf::Color(0,0,0));
sf::Text text;
text.SetCharacterSize(20);
text.SetColor(sf::Color(30, 255, 30));
unsigned int i;
for(i = 0; i < menuSize; i++){
text.SetString(menuText[i]);
text.SetPosition(10.f, i*25.f+10.f);
window.Draw(text);
}
text.SetString("Input: " + input);
text.SetPosition(10.f, i*25.f+30.f);
window.Draw(text);

window.Display();
window.SetActive(false);
}


If I won't call this from my main function, the program ends without error. Playing with those commands I found that the only line causing trouble is:
Code: [Select]
window.Draw(text); If you comment both of them away, text is not displayed but no error occurs on closing.

It may help to say that I initialize window this way:
Code: [Select]
window.Create(sf::VideoMode(500, 300, 32), "Settings", sf::Style::Titlebar);
and close like this:
Code: [Select]
void SettingsUI::Close(){
window.Close();
menuText.clear();
waveEngine = 0;
}


Also note that
Code: [Select]
std::vector<sf::String> menuText;
sf::String input;

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Closing window causes access violation
« Reply #4 on: October 21, 2011, 02:11:05 pm »
It's probably the known bug of the default font which causes a crash at global exit.
Laurent Gomila - SFML developer

Beta_Ravener

  • Jr. Member
  • **
  • Posts: 51
    • ICQ Messenger - 271426715
    • View Profile
Closing window causes access violation
« Reply #5 on: October 21, 2011, 02:22:34 pm »
Then it should be true, that default font is loaded only if no other font is bind to sf::Text while calling sf::RenderWindow::Draw ? If so, would loading my own font to sf::Font and using this one prevent the crash?

And why closing console window doesn't result it crash? The console window, however, stays stuck for some time, and at last the programs ends returning exit code -1073741510 (but at the end of main I return 0).

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Closing window causes access violation
« Reply #6 on: October 21, 2011, 02:43:01 pm »
Quote
Then it should be true, that default font is loaded only if no other font is bind to sf::Text while calling sf::RenderWindow::Draw ? If so, would loading my own font to sf::Font and using this one prevent the crash?

To prevent the crash, the default font must never be instanciated, so you have to pass your own font to the constructor of all your sf::Text instances.
Doing it with the SetFont function is already too late.

Quote
And why closing console window doesn't result it crash?

Closing the console terminates the program in a very ugly way -- nothing gets destructed properly. So no "crash", but memory leaks etc.
Laurent Gomila - SFML developer

Beta_Ravener

  • Jr. Member
  • **
  • Posts: 51
    • ICQ Messenger - 271426715
    • View Profile
Closing window causes access violation
« Reply #7 on: October 21, 2011, 03:26:24 pm »
Quote from: "Laurent"
Closing the console terminates the program in a very ugly way -- nothing gets destructed properly. So no "crash", but memory leaks etc.


That's quite disturbing as I'm planning to use console in final release.. when we are at it, is there any way to disable all the options to close console?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Closing window causes access violation
« Reply #8 on: October 21, 2011, 03:27:52 pm »
Quote
That's quite disturbing as I'm planning to use console in final release..

That's fine, just don't close it directly.

Quote
when we are at it, is there any way to disable all the options to close console?

I don't think that Windows allows this.
Laurent Gomila - SFML developer

Beta_Ravener

  • Jr. Member
  • **
  • Posts: 51
    • ICQ Messenger - 271426715
    • View Profile
Closing window causes access violation
« Reply #9 on: October 21, 2011, 03:48:58 pm »
Thanks for the help.

And one more thing, though this might be already known. I didn't want to have to include font with my program and as the built-in arial was pretty good, I just did:
Code: [Select]
sf::Font font = sf::Font::GetDefaultFont();
and this one I passed into sf::Text constructor. The result is same as before, but no crash anymore. As I read in that other thread, you got problems with those global variables, that can't be initialized before entering main function. Maybe you could have a class inside SFML that groups all the globals and something like sf::Init() that would user call from body of his main? It's the very same as glewInit() I need to call before I can use many of openGL calls, and there probably isn't better way to do this.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Closing window causes access violation
« Reply #10 on: October 21, 2011, 03:59:24 pm »
Quote
The result is same as before, but no crash anymore.

That's... strange :?

Quote
As I read in that other thread, you got problems with those global variables, that can't be initialized before entering main function. Maybe you could have a class inside SFML that groups all the globals and something like sf::Init() that would user call from body of his main? It's the very same as glewInit() I need to call before I can use many of openGL calls, and there probably isn't better way to do this.

It's a little bit more complicated ;)
Laurent Gomila - SFML developer

slotdev

  • Sr. Member
  • ****
  • Posts: 385
    • View Profile
Closing window causes access violation
« Reply #11 on: November 09, 2011, 02:54:25 pm »
Laurent

I really need a fix for this text bug very soon. I can guarantee that I don't need the default font, so can I comment out some code in the libraries to stop the default font being instanciated? If so, can you tell me which lines?

I don't care what I have to do to fix this, but I need to do it now!
SFML 2.1

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Closing window causes access violation
« Reply #12 on: November 09, 2011, 03:39:03 pm »
You can comment every call to Font::GetDefaultFont (should occur in sf::Text constructors only), and assign NULL instead.
Laurent Gomila - SFML developer

slotdev

  • Sr. Member
  • ****
  • Posts: 385
    • View Profile
Closing window causes access violation
« Reply #13 on: November 09, 2011, 04:13:51 pm »
So what should the Text.hpp say?

Code: [Select]

explicit Text(const String& string, const Font& font = Font::GetDefaultFont(), unsigned int characterSize = 30);


I assume it needs to call a function to just return a NULL sf::Font ??
SFML 2.1

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Closing window causes access violation
« Reply #14 on: November 09, 2011, 04:25:10 pm »
Just remove the default argument. If you really need to construct sf::Text instances from just a string, add a new constructor:
Code: [Select]
Text(const String& string);
Laurent Gomila - SFML developer