Hello!
I use SFML 2.0 RC (17.05.2012).
This program opens new subwindow when main window is clicked.
#include<SFML\Graphics.hpp>
#include <vector>
#include<iostream>
#include <Windows.h>
std::vector < std::pair< sf::Thread * , int> > threadArray;
void loop( int id)
{
//empty window
sf::RenderWindow window(sf::VideoMode(800, 400, 32), "", 4);
while ( window.isOpen() )
{
sf::Event event;
while (window.pollEvent(event))
{
if ((event.type == sf::Event::Closed) ||
((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape)))
{
window.close();
break;
}
}
window.clear(sf::Color::White);
window.display();
}
//mark thread as ended
for( int i=0; i<threadArray.size(); i++ )
{
if(threadArray[i].second == id)
threadArray[i].second = -1;
}
};
int main()
{
sf::RenderWindow window( sf::VideoMode(800, 400, 32), "memory leak example", 4);
window.setPosition(sf::Vector2i( 0,0) );
int threadId = 0;
while ( window.isOpen() )
{
sf::Event event;
while (window.pollEvent(event))
{
if ((event.type == sf::Event::Closed) ||
((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape)))
{
window.close();
break;
}
if(event.type == event.MouseButtonPressed)
{
if(event.mouseButton.button == sf::Mouse::Left)
{
int i=0;
//use ended
for(; i < threadArray.size(); i++)
{
if( threadArray[i].second == -1 )
{
threadArray[i].first->launch();
threadArray[i].second = i;
break;
}
}
//or create new thread
if( i== threadArray.size() )
{
threadArray.push_back( std::pair< sf::Thread* , int >( new sf::Thread( &loop, threadId ), threadId ) );
threadArray.back().first->launch();
Sleep(100);
threadId++;
}
}
}
}
window.clear(sf::Color::White);
window.display();
}
return EXIT_SUCCESS;
}
It works, but I noticed memory leak about 3mb per new subwindow.
I suppose it's incorrect to store threads in dynamic memory, but I have not any idea how to do it differently.
How can I solve this problem?
Thank you for your help and sorry for my English.