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

Author Topic: Memory leak when using text.getLocalBounds ()  (Read 2377 times)

0 Members and 1 Guest are viewing this topic.

Tima32

  • Newbie
  • *
  • Posts: 21
    • View Profile
    • Email
Memory leak when using text.getLocalBounds ()
« on: January 10, 2018, 07:19:51 pm »
при использовании "text[*Number_of_lines].getLocalBounds()" происходит утечка памяти, в чем может быть причина?
Constructor:
ContextMenu::ContextMenu(Font &_font, int x, int y)
{
        Background = new RectangleShape;
        Highlighted_field = new RectangleShape;
        CStandart = new Color(27, 27, 28);
        GuidanceColor = new Color(45, 45, 48);
        Number_of_lines = new short int(0);
        text = (Text*) malloc (0 * sizeof (Text));
        font = new Font(_font);
        positionX = new int(x);
        positionY = new int(y);
        width = new float(0);
        height = new float(1);
        Visible = new bool(1);

        Background->setPosition(x, y);
        Background->setFillColor(*CStandart);

        Highlighted_field->setFillColor(Color(45, 45, 48));
        Highlighted_field->setPosition(x+1, y+1);
}
Destructor:
delete Background;
        delete Highlighted_field;
        delete CStandart;
        delete GuidanceColor;

        for (int i = 0; i < *Number_of_lines; i++)
        {
                text[i].~Text();
        }
        free(text);
        //operator delete[] (text);
        delete Number_of_lines;

        delete font;
        delete positionX;
        delete positionY;
        delete width;
        delete height;
        delete Visible;
Function with memory leak:
text = (Text*) realloc (text, (*Number_of_lines + 1) *  sizeof (Text));
        new (text + *Number_of_lines) Text;

        text[*Number_of_lines].setString(PText);
        text[*Number_of_lines].setFont(*font);
        text[*Number_of_lines].setCharacterSize(11);
        text[*Number_of_lines].setColor(Color(255, 255, 255));
        text[*Number_of_lines].setPosition(*positionX + 4, *positionY + 15 * *Number_of_lines);

        //*height = *height + 15;

        //There is a memory leak!
        FloatRect *Bounding_Rectangle = new FloatRect(text[*Number_of_lines].getLocalBounds());
        cout << Bounding_Rectangle->width << ", " << Bounding_Rectangle->height <<endl;
        //int temp_width = (Bounding_Rectangle->width + 8) * 1.02;
        //if(*width < temp_width)
        //{
        //      *width = temp_width;
        //}
        //Background->setSize(Vector2f(*width, *height + 1));//"+1"&#1088;&#1072;&#1084;&#1082;&#1072;
        delete Bounding_Rectangle;

        //Highlighted_field->setSize(Vector2f(*width - 2, 15));

        //*Number_of_lines = *Number_of_lines + 1;
        return *Number_of_lines;
Finally, a class call:
while(1)
        {
                ContextMenu *Menu_test = new ContextMenu(font, 50, 60);
                Menu_test->addString(L"Test");
                delete Menu_test;
        }

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Memory leak when using text.getLocalBounds ()
« Reply #1 on: January 10, 2018, 07:32:31 pm »
You should write in English as this is an English forum. ;)

This is some of the more horrendous code I've seen in a while.
C++ doesn't require you to allocate everything dynamically. Just use member variables.
C++ also isn't C# or Java where you create an instance by calling new.

You can not construct a C++ object with malloc, it simply doesn't work and is most certainly the issue of your "memory leak".

However the real issue here is, that you're trying to write C++ but it's wrong in pretty much any way possible. Please learn some proper C++ first, before trying to use a library or even look for memory leaks.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Tima32

  • Newbie
  • *
  • Posts: 21
    • View Profile
    • Email
Re: Memory leak when using text.getLocalBounds ()
« Reply #2 on: January 11, 2018, 04:17:30 pm »
I apologize for the unreadable code, it is crippled in attempts to fix the problem.
A memory leak occurs when you call "FloatRect Bounding_Rectangle = text [* Number_of_lines] .getLocalBounds ();" If this line is removed, then there will be no memory leak.
Changed the code, but to no avail.
Background = new RectangleShape;
        Highlighted_field = new RectangleShape;
        CStandart = new Color(27, 27, 28);
        GuidanceColor = new Color(45, 45, 48);
        Number_of_lines = new short int(0);
        //text = (Text*) malloc (0 * sizeof (Text));
        text = new Text[20];
        font = new Font(_font);
        positionX = new int(x);
        positionY = new int(y);
        width = new float(0);
        height = new float(1);
        Visible = new bool(1);

        Background->setPosition(x, y);
        Background->setFillColor(*CStandart);

        Highlighted_field->setFillColor(Color(45, 45, 48));
        Highlighted_field->setPosition(x+1, y+1);

text[*Number_of_lines].setString(PText);
        text[*Number_of_lines].setFont(*font);
        text[*Number_of_lines].setCharacterSize(11);
        text[*Number_of_lines].setColor(Color(255, 255, 255));
        text[*Number_of_lines].setPosition(*positionX + 4, *positionY + 15 * *Number_of_lines);

        FloatRect Bounding_Rectangle = text[*Number_of_lines].getLocalBounds();


        return *Number_of_lines;
Also, I tried to put the code in the function "main" and there were no memory leaks in it.
while(1)
        {
                Text *text;
                text = (Text*) malloc (0 * sizeof (Text));
                text = (Text*) realloc (text, (1) *  sizeof (Text));
                new (text + 0) Text();
                text[0].setString("123");
                FloatRect *Bounding_Rectangle = new FloatRect(text[0].getLocalBounds());
                text[0].~Text();
                delete Bounding_Rectangle;
                free(text);
        }

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Memory leak when using text.getLocalBounds ()
« Reply #3 on: January 11, 2018, 04:58:07 pm »
Until you can write proper C++ code without pointless new calls and even worse trying to create instances with malloc, I won't be spending any time trying to guess a "memory leak".

It's like you're standing in the middle of a river and you're afraid to get wet from the rain.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Tima32

  • Newbie
  • *
  • Posts: 21
    • View Profile
    • Email
Re: Memory leak when using text.getLocalBounds ()
« Reply #4 on: January 11, 2018, 11:53:50 pm »
If you call this code, a memory leak occurs:
Font font;
        font.loadFromFile("tahoma.ttf");

        while(1)
        {
                Text text;
                Font _font = font;

                text.setString(L"Text");
                text.setFont(font);
                text.setCharacterSize(11);
                text.setColor(Color(255, 255, 255));
                FloatRect Bounding_Rectangle = text.getLocalBounds();
        }

If you remove the line
Font _font = font;
or
FloatRect Bounding_Rectangle = text.getLocalBounds ();
then the memory leak disappears. Why is this happening? :-\

Tima32

  • Newbie
  • *
  • Posts: 21
    • View Profile
    • Email
Re: Memory leak when using text.getLocalBounds ()
« Reply #5 on: January 11, 2018, 11:58:32 pm »
Why are they not friends?

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: Memory leak when using text.getLocalBounds ()
« Reply #6 on: January 12, 2018, 07:54:20 am »
If you stop using dynamic allocation for no reason at all, you'll stop getting memory leaks.