SFML community forums
Help => General => Topic started by: ptrxyz on May 19, 2009, 12:26:54 am
-
Hello.
I will start a new projekt next month and we are thinking about switching from SDL to SFML. As mentioned in several topics and in the todo-list there is a major issue with SFML crashing when the main program closes and you used drawtext (or however it's called...) before.
I myself would prefer to use SFML for that projekt but i wonder if that bug will be fixed during the next few days / weeks. I checked the todo-list and found out that the last edit was on 06 March =(. So, my question is if there is a chance that this bug will get fixed "soon" or if there is a work around for that crashing issue...
Does anyone know?
-
if there is a chance that this bug will get fixed "soon"
No.
if there is a work around for that crashing issue...
If you've read several topics about it, then you already know the workarounds ;)
-
So far avoid using the default sfml font and you're Ok. SFML is good and robust enough to be used both in free and commercial projects.
-
I tested with the sfml2 branch, and it seems that this bug doesn't appear anymore in this branch :)
-
Hm, yes, i read i should avoid the default font and it should work ... but seems it doesnt work for the .NET package (at least it doesnt for me.... O.O)
But i will try to use the SFML2 branch. It's this one, right? /branches/sfml2/dotnet/
Oh, just in case i am doing something wrong, here is the quick'n'dirty code i used for testing the crashing:
Imports SFML.Graphics
Imports SFML.Audio
Imports SFML.Window
Imports System
Module Module1
Dim WithEvents App As RenderWindow
Dim doit As Boolean = True
Public Sub Main()
Dim vMode As New VideoMode(640, 480, 32)
App = New RenderWindow(vMode, "Test", Styles.None Or Styles.Titlebar)
Dim img As New Image("somefile.png")
Dim sprite As New Sprite(img)
Dim fnt As Font = New Font("c:\windows\fonts\Calibri.ttf", 14)
Dim str2D As New String2D("Hello World", fnt, 14)
Dim ff As Long = 0
Dim st As Long = Now.TimeOfDay.TotalMilliseconds
Dim fps As Single = 0
App.UseVerticalSync(True)
App.SetFramerateLimit(60)
While App.IsOpened And doIt
App.Clear(New SFML.Graphics.Color(0, 0, 0, 1))
sprite.Position = New Vector2(sprite.Position.X + 1, 10)
If sprite.Position.X > 600 Then sprite.Position = New Vector2(0, 10)
sprite.BlendMode = BlendMode.Alpha
App.Draw(sprite)
sprite.Position = New Vector2(sprite.Position.X + 1, 100)
sprite.BlendMode = BlendMode.Multiply
App.Draw(sprite)
ff += 1
If Now.TimeOfDay.TotalMilliseconds - st >= 1000 Then
fps = (ff / (Now.TimeOfDay.TotalMilliseconds - st) * 1000)
'str2D.Dispose() 'Even if this line is commented out I get the crash.
'str2D = New String2D(fps, fnt, 14)
st = Now.TimeOfDay.TotalMilliseconds : ff = 0
End If
'App.Draw(str2D) 'No need to draw it...crash occures anyways.
App.Display()
App.DispatchEvents()
End While
str2D.Dispose()
fnt.Dispose()
fnt = Nothing
str2D = Nothing
App.Close
End Sub
Private Sub App_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles App.Closed
doit = False
'App.Close() '<- no need here. Releaseing Resources in Sub Main...
End Sub
Private Sub App_KeyPressed(ByVal sender As Object, ByVal e As SFML.Window.KeyEventArgs) Handles App.KeyPressed
doit = False
'App.Close() '<- no need here. Releaseing Resources in Sub Main...
End Sub
End Module
Edit:
I compiled the .NET libs i found here: /branches/sfml2/dotnet/
I tried my little testprogram again...still same error. =(
Maybe someone can tell me what I am doing wrong? I uploaded everything I used for testing (Test-Project and the latestet SFML2 dotnet-library): http://www.itak.de/share/SFMLTest.rar
-
but seems it doesnt work for the .NET package
Yes, in SFML.Net the internal sf::String of the String2D class is always constructed with the default font first.
I compiled the .NET libs i found here: /branches/sfml2/dotnet/
I tried my little testprogram again...still same error. =(
You have to recompile SFML (static release), CSFML (dynamic release) and then SFML.Net.
-
but seems it doesnt work for the .NET package
Yes, in SFML.Net the internal sf::String of the String2D class is always constructed with the default font first.
I compiled the .NET libs i found here: /branches/sfml2/dotnet/
I tried my little testprogram again...still same error. =(
You have to recompile SFML (static release), CSFML (dynamic release) and then SFML.Net.
Ok, i recompiled it like you said i should. Then i copied the new files (csfml-???.dll) to dotnet/extlibs/ and recompiled the dotnet-projekt. Then i copied all new libs (csfml-???.dll and fsmlnet-???.dll) to my test project and set up the links correctly. the problem got partly solved:
running the executable out of the IDE (VS2008, Debugging mode) --> error on exit
running the executable (release-build, not out of IDE) --> no error on exit
running the executable (debug-build, but not out of IDE) --> no error on exit
I figured out that the error is still caused by the string2D class. As long as i dont create that class, i wont get an error. but maybe it's just an IDE issue? Any ideas anyone?
edit: Disabeling the VS-Host-Process fixes the error, too...
-
I found that deleting the main RenderWindow object is the reason.
I don't know if in Visual Basic you can just let it alive, but in C++ I just never free it and I'm doing OK.
I'm most certain that it has to do with the OpenGL context. Even if there is a global context until the application exits, the Window context dies when closing the RenderWindow.
And when the default font tries to free it's textures, it crashes.
I've already posted that if the library skips the wglDeleteContext in the WM_DESTROY event... SFML wont crash.
-
I don't know if in Visual Basic you can just let it alive, but in C++ I just never free it and I'm doing OK.
Hm, I think there is no way to keep it alive since .NET destroys it automatically i guess.
I've already posted that if the library skips the wglDeleteContext in the WM_DESTROY event... SFML wont crash.
I will comment out those lines and recompile SFML and see what happens.
But isnt it strange that deactivating the VS.NET's debug host process fixes the crashes too...?
-
It's kind of weird. But in my case it happens with Native C++ (VC++ Express 2008).
Even in release mode, so in my case it's not a debug thing.
I found that removing the "delete myWindow;" in Window::Close()
(Window.cpp, line 167) solved this.
But in debug mode it could whine about memory leaks.
Good luck!
-MartÃn
-
but seems it doesnt work for the .NET package
Yes, in SFML.Net the internal sf::String of the String2D class is always constructed with the default font first.
I've deleted all references to DefaultFont i C# lib (even import function of default font getter from c lib). So now, there is no initialization of font in C# package. I've recompiled only C# package. I've still have problem of making this work. There is no way to omit that problem in simple way on C# for 1.5 version?
PS: That my first post, so I would like to say... gr8 JOB, that Lib is really gr8.
-
The default font is instancianted implicitely when sfString_Create is called. And this function is the only way to create a string, so I'm afraid there's no immediate solution to this problem with SFML 1.5.
-
In sfml 2.0:
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
struct structure
{
structure() :
font(sf::Font::GetDefaultFont())
{
}
sf::Font font;
};
static structure s_structure;
int main()
{
//===========================================
// Create the main window
sf::Window App(sf::VideoMode(800, 600, 32), "SFML Events");
// Get a reference to the input manager associated to our window, and store it for later use
const sf::Input& Input = App.GetInput();
// Start main loop
while (App.IsOpened())
{
// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close();
// Escape key : exit
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();
}
// Display window on screen
App.Display();
}
//==================================
return EXIT_SUCCESS;
}
It leave an openGL error with and without the section between the equals.
I've also tried with image but it seem to be ok ^^.
-
Using SFML outside main is dangerous ;)
-
It's an example. The problem is that i made a little gui with a template loader and i use a static templateManager that i fill with the config in a xml file.
The templateManager contain structure and are used in all element to initialise their parameter with the content of the structure. So it cause me an error!
So if sfml doesn't support the use of static member it's a little sad.... (maybe more than little)....
I don't know how i will fix it..
-
That's why globals are bad. Nobody can control their order of initialization. You should really not load your template config before entering the main function.
-
I "LOAD" them in the main :P. The structure are just initialised before the main cause they are in a static class.
-
And what about destruction ? Sometimes you cannot allow the destruction at an unknown time. You may say there is always a solution but it need more work to do it well.
Another thing about global : using them show that your architecture is badly design.
Conclusion : forget global.
-
You shouldn't rely on the initialization order of static or extern variables in different modules because it is undefined.
Are you sure that nothing is accessed before it has been initialized? Check that with your debugger.
-
You shouldn't rely on the initialization order of static or extern variables in different modules because it is undefined.
Are you sure that nothing is accessed before it has been initialized? Check that with your debugger.
100% sure. Juste including the file in my project with NO CALL AT all on this raise the opengl error.
And what about destruction ? Sometimes you cannot allow the destruction at an unknown time. You may say there is always a solution but it need more work to do it well.
Another thing about global : using them show that your architecture is badly design.
Conclusion : forget global.
lolll easy conclusion.. How will you implement a global templating without global variable? Huh? Somethime global are needed or useful. My template manager need to be accessed by all elements to provide them their initial value.
-
Juste including the file in my project with NO CALL AT all on this raise the opengl error.
Which file??
-
The class (sorry).
The problem is that when the program clause and sfml free texture it raise an error on that font.
-
lolll easy conclusion..
Not at all.
How will you implement a global templating[1] without global variable?
Like this :
class Manager { ... };
int main(int, char**) {
Manager mainmanager(...);
ModuleOne m1(mainmanager, ...);
ModuleTwo m2(mainmanager, ...);
...
ModuleN mn(mainmanager, ...);
...
As you see I dislike singleton.
[1] : what d'you mean exactly ? [ 'cause my dictionary doesn't know this word :wink: . ]
-
An easy fix for you:
1/ If the crash is happening at global startup, instanciate your singleton on demand rather than at global startup
class TemplateManager
{
public:
static TemplateManager& instance()
{
static TemplateManager manager;
return manager;
}
};
2/ If the crash is happening at global exit, create a function to free resources so that you can control the moment it is done. In fact you always want to control when your resources are cleaned up, trust me ;)
On big projects with tons of modules handling resources and dependencies everywhere, your (lack of) design would never work.
-
An easy fix for you:
1/ If the crash is happening at global startup, instanciate your singleton on demand rather than at global startup
class TemplateManager
{
public:
static TemplateManager& instance()
{
static TemplateManager manager;
return manager;
}
};
2/ If the crash is happening at global exit, create a function to free resources so that you can control the moment it is done. In fact you always want to control when your resources are cleaned up, trust me ;)
On big projects with tons of modules handling resources and dependencies everywhere, your (lack of) design would never work.
I will see what i can do with that.
PS: The program dont crash. It just leave an opengl exception at the end.
-
The error doesn't occur if we do:
font(sf::Font())
instead of
font(sf::Font::GetDefaultFont())
I don't know why, i don't know if it can help to understand anything.. I'm happy with that.
-
Just for informations: I modified a LITTLE bit my desing to make templateManager to be a singleton and manager ressources a little better and now all work fine :). (with or without the GetDefaultFont()).