SFML community forums

Help => System => Topic started by: valerion on September 29, 2015, 11:19:00 am

Title: leak ram
Post by: valerion on September 29, 2015, 11:19:00 am
sorry for my endlish... :-[
At first it was so
(click to show/hide)
(click to show/hide)
screen 1: "Failed to activate the window's context"

after correction:
void Main::THREAD_Draw()
void Main::THREAD_Draw()
{
        window->draw(*shape1);
        window->draw(*shape2);
        window->setActive(false);
}
it was like on screen 2: leak RAM
Title: Re: leak ram
Post by: Jesper Juhl on September 29, 2015, 11:24:10 am
Don't use raw owning pointers. Use std::unique_ptr (or std::shared_ptr) for owning pointers. Only use raw pointers for observing non-owning pointers.
Also look up RAII.
Title: Re: leak ram
Post by: eXpl0it3r on September 29, 2015, 11:35:30 am
And don't use threads. Whatever you think you'd gain, you think wrong.

Are you coming from a Java or C# background, because as Jesper Juhl said, you should use RAII (http://www.bromeon.ch/articles/raii.html).
Title: Re: leak ram
Post by: valerion on September 29, 2015, 06:36:49 pm
Don't use raw owning pointers. Use std::unique_ptr (or std::shared_ptr) for owning pointers. Only use raw pointers for observing non-owning pointers.
Also look up RAII.
this code is not my, if use shared_ptr problem remains. I agree with you, but problem not in it
Title: Re: leak ram
Post by: valerion on September 29, 2015, 06:47:57 pm
And don't use threads. Whatever you think you'd gain, you think wrong.

Are you coming from a Java or C# background, because as Jesper Juhl said, you should use RAII (http://www.bromeon.ch/articles/raii.html).
then i used thread in training purposes
Title: Re: leak ram
Post by: Nexus on September 29, 2015, 10:08:06 pm
this code is not my, if use shared_ptr problem remains.
The idea is not to use shared pointers. Use pure objects wherever possible, not pointers.

I agree with you, but problem not in it
You talk about memory leaks and show a code that blatantly provokes them... So this is hard to believe.

If you're sure that the problem persists, then show the new code. If you write it correctly, the code won't contain a single new keyword.

By the way, to detect memory leaks, you should use a tool designed for that task, not the Windows task manager.
Title: Re: leak ram
Post by: valerion on October 02, 2015, 11:14:37 pm
this code is not my, if use shared_ptr problem remains.
The idea is not to use shared pointers. Use pure objects wherever possible, not pointers.

I agree with you, but problem not in it
You talk about memory leaks and show a code that blatantly provokes them... So this is hard to believe.

If you're sure that the problem persists, then show the new code. If you write it correctly, the code won't contain a single new keyword.

By the way, to detect memory leaks, you should use a tool designed for that task, not the Windows task manager.
tool, for example?
Title: Re: leak ram
Post by: valerion on October 02, 2015, 11:18:37 pm
new code without pointers :D
main.h
(click to show/hide)

main.cpp
(click to show/hide)
old problem remains
Title: Re: leak ram
Post by: valerion on October 02, 2015, 11:25:52 pm
on one forume i got this answer:
Problem is that when creating in thread, creates a hidden context opengl and added to "global basked"(std::set <GlContext*> internalContext) and at destruction thread isn`t released.
Title: Re: leak ram
Post by: zsbzsb on October 03, 2015, 03:14:19 pm
Yes it is true that SFML will create a context on every thread that you use to preform graphics operations. The solution to fix this is to simply reuse the same thread.

Now it is also true that you do not understand how threads work and when to use them. When you launch a new thread every frame and then block the main thread until that other thread finishes you gain absolutely none, zero, zip performance from your code. In fact you will actually hurt the performance of your code as all you do is simply add more overhead (creating, launching, switching threads is expensive operations).

The purpose of threads is to execute code in parallel, but if you aren't even running code in parallel you are just adding unnecessary overhead to your code for no reason. Oh, and once you actually get code running in parallel you need to learn how to protect memory so it doesn't access memory that another thread is using. That in itself is a huge topic and has many pitfalls (even if you execute code in parallel you may lose performance because of memory protection).

What you should do is forget you even heard of multithreading and make your entire program single threaded. Once you actually have a finished program use a profiler to see where your actual bottlenecks lie and then optimize that code (it probably won't even involve threads).
Title: Re: leak ram
Post by: valerion on October 04, 2015, 01:01:58 am
The solution to fix this is to simply reuse the same thread.
Bad solution. If i use a lot of draw function and other function, work with lists, in main thread, all this very loaded and becomes visible small hang. Solution was to divide all of different thread.
Title: Re: leak ram
Post by: zsbzsb on October 04, 2015, 02:11:42 am
Bad solution. If i use a lot of draw function and other function, work with lists, in main thread, all this very loaded and becomes visible small hang. Solution was to divide all of different thread.

Read my reply again and again until you understand it. Launching a new thread and then blocking until it is finished every single frame will not get you anymore performance (this does not matter if you are using SFML or anything else). It isn't even how you are supposed to use threads. And as for reusing a thread, its the only solution to prevent SFML from creating a new context per thread.
Title: Re: leak ram
Post by: Gambit on October 04, 2015, 05:05:34 am
The solution to fix this is to simply reuse the same thread.
Bad solution. If i use a lot of draw function and other function, work with lists, in main thread, all this very loaded and becomes visible small hang. Solution was to divide all of different thread.

If your application is lagging when you are trying to draw a lot of stuff you should probably consider trying to reduce the amount of draw calls you are making. As for the threads, it has been said more than once that you are getting absolutely 0 benefit from using them considering you are using them incorrectly. I also dont believe that the threads you are using are actually making your program run any faster but instead are "masking" your problem and making it seem like its working.
Title: Re: leak ram
Post by: mkalex777 on October 19, 2015, 05:34:03 am
Completely agree, using multithreaded game loop is very complicated solution and you need to understand all details about thread synchronization, thread context switch, how works system scheduler, what is time slice, how to switch windows to realtime mode and why it gives faster respose time, but slower performance and many other things.
Single threaded game loop is very simple and gives you a lot of benefits in performance. So, you are need for a very significant reason to use multithreading, becuse you will loss ability to use simple and fast code and your code will be complicated.