SFML community forums
Help => Graphics => Topic started by: chocobn on April 08, 2011, 01:24:50 am
-
Hello! I get an error while using sf::RenderImage. Here is my minimal code :
std::cout<< "debutExport" <<std::endl;
m_app->canvasWindow->canvas->getFlattenedImage();
std::cout<< "finExport" <<std::endl;
sf::Image CE_Group::getFlattenedImage()
{
std::cout<< "start" <<std::endl;
sf::RenderImage RI;
RI.Create(800,600);
RI.Clear(sf::Color(200,200,200));
RI.Display();
sf::Image img = RI.GetImage();
std::cout<< "end" <<std::endl;
return img;
}
I really don't understand because this what the programs displays :
debut export. total of frames:1000
debutExport
start
end
Segmentation fault
Does anyone have an idea about what i did wrong? The point is that the error is at the end of the method... I really don't understand why it occurs after the "return"...
Thanks in advance.
-
You should rather use gdb and display the call stack. The crash probably happens during the destruction of the render image.
-
Gdb doesn't want to display the call stack ! I'm using the GDB geany plugin. It seems so strange to me to can't succeed to display a call stack in that case... :(
Indeed, it looks like it is during the destruction of the renderimage, because if I create it via a pointer (i don't know how this is really called), it works, but the renderimage is not destroyed!
sf::RenderImage* RI = new sf::RenderImage();
-
You must use a debug version of SFML.
-
Sorry i'm such a newbie but i never did that kind of things! I'm using cmake. How could i change the libraries for the debug versions?
There are the concerned parts of my cmake file :
find_package(SFML REQUIRED COMPONENTS graphics window system)
target_link_libraries(Programme ${SFML_SYSTEM_LIBRARY} ${SFML_AUDIO_LIBRARY} ${SFML_GRAPHICS_LIBRARY} ${SFML_WINDOW_LIBRARY} ${GTKMM_LIBRARIES})
i tried target_link_libraries(Programme ${SFML_SYSTEM_LIBRARY} ${SFML_AUDIO_LIBRARY} ${SFML_GRAPHICS_LIBRARY_DEBUG} ${SFML_WINDOW_LIBRARY} ${GTKMM_LIBRARIES})
but it doesn't works...
-
You must compile the debug SFML libraries (with CMAKE_BUILD_TYPE = Debug), and then in your project it's better to link to the debug version of all modules.
-
ok, I did that, but it doesn't change anything... I still have this segfault. In my mind, it is at the destruction of the renderImage... Something in the destructor of renderImage that bugs, maybe because of my code. But I might be wrong
-
ok, I did that, but it doesn't change anything...
That should work.
What if you run gdb from the command line?
-
I will try, but it may take some time because i never did that and i have to learn how it works.
Is there a way to ensure that i am using the debug libraries?
I have compiled sfml in debug and changed my project's cmakelists.txt file to :
[...]
target_link_libraries(Programme ${SFML_SYSTEM_LIBRARY_DEBUG} ${SFML_AUDIO_LIBRARY_DEBUG} ${SFML_GRAPHICS_LIBRARY_DEBUG} ${SFML_WINDOW_LIBRARY_DEBUG} ${GTKMM_LIBRARIES})
[...]
-
Actually, i succeed easily to use gdb... here is the backtrace, directly from the console :
(gdb) backtrace
#0 0x0166d675 in xcb_writev () from /usr/lib/libxcb.so.1
#1 0x01488dec in _XSend () from /usr/lib/libX11.so.6
#2 0x01488f90 in _XReply () from /usr/lib/libX11.so.6
#3 0x0147c867 in XSync () from /usr/lib/libX11.so.6
#4 0x013ac205 in ?? () from /usr/lib/fglrx/libGL.so.1
Backtrace stopped: previous frame inner to this frame (corrupt stack?)
What does mean the last line? Is it the source of the problem?
-
Hmm... it's not really helpful (there's no function from SFML or your own code).
Ok, let's try something else. Could you show me a complete and minimal code thatreproduces your problem? I insist on complete and minimal ;)
-
hoping it is as minimal as expected! (at least it's complete I think ;) )
thank you very much for your help!
main.cpp
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <iostream>
sf::Image getFlatennedImage()
{
std::cout<< "debutFonction" <<std::endl;
sf::RenderImage RI;
RI.Create(800,600);
RI.SetActive();
RI.Clear();
RI.Display();
std::cout<< "finFonction" <<std::endl;
return RI.GetImage();
}
int main()
{
// Create the main window
sf::Window App(sf::VideoMode(800, 600, 32), "SFML Window");
// Start main loop
bool Running = true;
while (Running)
{
std::cout<< "avant appel" <<std::endl;
getFlatennedImage();
std::cout<< "apres appel" <<std::endl;
App.Display();
}
return EXIT_SUCCESS;
}
cmakelists.txt (not really needed, but may be helpful)
# explications qt = http://www.marcossantamaria.com/tutorialdata.php?id=3
cmake_minimum_required(VERSION 2.8)
set(CMAKE_CXX_FLAGS "-Wall -W -ansi -pedantic -g")
set(CMAKE_BUILD_TYPE CMAKE_CXX_FLAGS_DEBUG)
#Déclaration du projet
project(Programme)
find_package(SFML REQUIRED COMPONENTS graphics window system)
#Déclaration de l'exécutable
add_executable(
Programme
main.cpp
)
target_link_libraries(Programme ${SFML_SYSTEM_LIBRARY_DEBUG} ${SFML_AUDIO_LIBRARY_DEBUG} ${SFML_GRAPHICS_LIBRARY_DEBUG} ${SFML_WINDOW_LIBRARY_DEBUG})
INSTALL_TARGETS( /bin Programme)
-
Try to add an event loop that process event from the window (it doesn't have to do something useful, just call GetEvent).
And is the first call crashing? Or is it happening after a certain amount of calls?
-
it does exactly that
$ ./Programme
avant appel
debutFonction
finFonction
Segmentation fault
even with adding:
sf::Event Event;
while (App.GetEvent(Event))
{
// Process event
}
into the loop
-
Which revision of SFML 2 are you using?
Does the "shader" example work?
-
Looking at your code, you are creating the RenderImage as a temporary local variable in your function, then passing back a Sprite that uses it. The problem is that your RenderImage is then destroyed when the function returns, so the Sprite is no longer valid (it is based on a now deleted RenderImage).
Right?
-
Where do you see a sprite??
Anyway, the image is returned by copy, it's not a reference to the render-image's internal one. So as long as the calling code stores the result it's fine.
-
I think the very minimal code that reproduce the problem is the following :
#include <SFML/Graphics.hpp>
int main(int, char**) {
{ sf::RenderImage ri; ri.Create(100, 100); }
{ sf::RenderImage ri; ri.Create(100, 100); }
return 0;
}
Can you confirm this ?
If I'm right the line of code I sent you, Laurent, should fix that.
-
Where do you see a sprite??
Anyway, the image is returned by copy, it's not a reference to the render-image's internal one. So as long as the calling code stores the result it's fine.
Doh! That's what I get for reading too quickly, lol.
-
If I'm right the line of code I sent you, Laurent, should fix that.
I don't think so, for two reasons:
- it happens on the first call, not the second one
- there's a window that lives in main() so the internal OpenGL context is not destroyed between two calls to getFlatennedImage()
-
Ho, you're right! Apparently I read the topic too quickly. My bad.
-
@Hiura : yeah it does the same segfault!
So, does anyone know what am i doing wrong? Or is it a sfml bug?
Laurent i haven't found the shader example your talking about --'
I've upgraded my sfml version yesterday or the day before. I can upgrade it again, if it is helpful.
I downloaded a snapshot with a folder inside called "LaurentGomila-SFML-cb1f938". I think it's the version number that you want.
-
Your code works fine for me with Visual Studio. So I don't think, that it is a SFML-Bug. Probably there is something wrong with your libs(or Code Blocks doesn't like your code ;) - but I rather don't think so) or build-options. Unfortunately I have not enough experience with Code Blocks, to give you any advice. But I definitely don't think, that there is something wrong with SFML.
-
I'm using Make and gcc on an quite up to date Ubuntu Linux... It shouldn't be like that, that is definitly weird :/
I tried to make a minimal cmakelist.txt
set(CMAKE_BUILD_TYPE CMAKE_CXX_FLAGS_DEBUG)
project(Programme)
find_package(SFML REQUIRED COMPONENTS graphics)
add_executable(
Programme
main.cpp
)
target_link_libraries(Programme ${SFML_GRAPHICS_LIBRARY_DEBUG})
INSTALL_TARGETS( /bin Programme)