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

Author Topic: leak ram  (Read 8885 times)

0 Members and 1 Guest are viewing this topic.

valerion

  • Newbie
  • *
  • Posts: 7
    • View Profile
leak ram
« 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

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: leak ram
« Reply #1 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: leak ram
« Reply #2 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

valerion

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: leak ram
« Reply #3 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

valerion

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: leak ram
« Reply #4 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.
then i used thread in training purposes

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: leak ram
« Reply #5 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.
« Last Edit: September 29, 2015, 10:10:30 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

valerion

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: leak ram
« Reply #6 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?

valerion

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: leak ram
« Reply #7 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

valerion

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: leak ram
« Reply #8 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.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: leak ram
« Reply #9 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).
« Last Edit: October 03, 2015, 03:20:55 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

valerion

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: leak ram
« Reply #10 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.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: leak ram
« Reply #11 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.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: leak ram
« Reply #12 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.

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: leak ram
« Reply #13 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.
« Last Edit: October 19, 2015, 05:40:52 am by mkalex777 »