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

Author Topic: Memory leak  (Read 2484 times)

0 Members and 1 Guest are viewing this topic.

kimerafusion

  • Newbie
  • *
  • Posts: 2
    • View Profile
Memory leak
« on: August 22, 2014, 12:12:09 am »
Hey there, got a problem in some code, I saw that I had a mem leak (using Task Manager), and when I started looking everywhere by commenting 99% of my code I saw that the mem leak was supposed to be in this part of code :
void RectGrid::DrawGrid(sf::RenderWindow *window) {
        sf::Sprite RectSprite = *new sf::Sprite();
        RectSprite.setTexture(*RectTexture);
        for (int x = 0; x < grid.size(); x++) {
                for (int y = 0; y < grid.at(x).size(); y++) {
                        window->draw(RectSprite);
                }
        }
}
RectTexture is nothing more than an sf::Texture* and grid is an std::vector<std::vector<RectType>> where RectTypeis an enum. window is a pointer on an sf::RenderWindow.
I really don't understand what I have done wrong in this code...

math1992

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
    • Email
Re: Memory leak
« Reply #1 on: August 22, 2014, 12:15:31 am »
When do you delete RectSprite? Also if you want to create a tile map (Or anything with a grid) use sf::VertexArray instead.

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Memory leak
« Reply #2 on: August 22, 2014, 12:25:10 am »
Quote
When do you delete RectSprite?
Never, just as he should. You do not delete autos.

 sf::Sprite RectSprite = *new sf::Sprite();
On this line, you create a new sprite, dynamically on the heap, then copy it into a local variable and then discard it, that discarded sprite is the leak. RectSprite like that itself is fine, it gets freed automatically. What is wrong is the = *new sf::Sprite() why do you do that?
Back to C++ gamedev with SFML in May 2023

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10823
    • View Profile
    • development blog
    • Email
Re: Memory leak
« Reply #3 on: August 22, 2014, 12:29:58 am »
If you play around with raw pointers and new/delete while barely understanding the concept it's only a question of when you'll write leaking code.
Use references whenever possible, don't do manual memory management and learn about RAII.

Also if you "somewhere" read this kind of code, I strongly advise you to drop that source, it will teach you more bad things than good things. If you're interested in a good C++ book to get  a solid base, check out this list.

Also the task manager is not a good or not a way at all to track memory leaks.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

kimerafusion

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Memory leak
« Reply #4 on: August 22, 2014, 12:36:27 am »
Oh thanks a lot, that's why. Yeah, probably something I did not understand properly while learning C++.

Mörkö

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Re: Memory leak
« Reply #5 on: August 22, 2014, 04:19:56 pm »
What about this?

#include <SFML/Graphics/Texture.hpp>
int main() {
        sf::Texture texture;
}

Quote from: valgrind
==6844== HEAP SUMMARY:
==6844==     in use at exit: 15,082 bytes in 68 blocks
==6844==   total heap usage: 5,418 allocs, 5,350 frees, 4,658,922 bytes allocated
==6844==
==6844== Searching for pointers to 68 not-freed blocks
==6844== Checked 2,635,864 bytes
==6844==
==6844== LEAK SUMMARY:
==6844==    definitely lost: 0 bytes in 0 blocks
==6844==    indirectly lost: 0 bytes in 0 blocks
==6844==      possibly lost: 0 bytes in 0 blocks
==6844==    still reachable: 15,082 bytes in 68 blocks
==6844==         suppressed: 0 bytes in 0 blocks
==6844== Reachable blocks (those to which a pointer was found) are not shown.
==6844== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==6844==
==6844== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
--6844--
--6844-- used_suppression:      2 glibc-2.5.x-on-SUSE-10.2-(PPC)-2a /usr/lib64/valgrind/default.supp:1286
==6844==
==6844== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)

I don't understand all the technical info that comes before that part, but as far as I know it should be concluding this:

Quote
All heap blocks were freed -- no leaks are possible

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Memory leak
« Reply #6 on: August 22, 2014, 04:36:34 pm »
In that case, you should probably run valgrind with --leak-check=full --show-leak-kinds=all and show us where the leaked blocks come from.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).