c ++ / SFML-valgrind报告中显示内存泄漏

问题描述 投票:0回答:1

我有这个小代码,可以使用SFML反复绘制形状。它会突然中止,并显示不同的错误消息,例如corrupted size vs. prev_size / Aborted (core dumped)munmap_chunk(): invalid pointer /Segmentation fault (core dumped)

我使用valgrind来跟踪看起来像一些内存泄漏的事件,但是该报告对我来说是个秘密。然而,就我所知,“ 绝对丢失:1块中4,096字节”不是一个好兆头。最令人困惑的是,当我通过valgrind运行它时,它不会中止。

我会继续调查,但是如果有人可以给我提示,那将是很好。

最诚挚的问候,

MC

g++ -std=c++11 ./k.cpp -o ./k -Wfatal-errors -lsfml-graphics -lsfml-window -lsfml-system

#include "SFML/Graphics.hpp"
#include <iostream>
#include <math.h>
#include <random>
#include <stdio.h>
#include <string>

using namespace std;

struct point {
 double x;
 double y;
};

struct curve {
 int index;
 point centerPoint;
 double radius;
 sf::ConvexShape shape;
 sf::Text curveName;
 point curveNamePosition;
};

curve computeCurve(point centerPoint, double radius) {
 unsigned short numberOfPoints = 200;
 curve curve;
 curve.shape.setPointCount(numberOfPoints);
 curve.centerPoint.x = centerPoint.x;
 curve.centerPoint.y = centerPoint.y;
 curve.radius = radius;
 double alpha = 2 * M_PI / numberOfPoints;
 unsigned short a = 1;
 point point;

 for (unsigned short i = 0; i < numberOfPoints + 1; i++) {
   point.x = radius * (0.5 + cos(2 * a * alpha * i) / 2) * cos(alpha * i) +
             centerPoint.x;
   point.y = radius * sin(a * alpha * i) + centerPoint.y;
   curve.shape.setPoint(i, sf::Vector2f(point.x, point.y));
 };

 for (unsigned short i = 0; i < numberOfPoints - 1; i++) {
   point.x = radius * (0.5 + cos(2 * a * alpha * i) / 2) * cos(alpha * i) +
             centerPoint.x;
   point.y = -radius * sin(a * alpha * i) + centerPoint.y;
   curve.shape.setPoint(2 * numberOfPoints - i,
                        sf::Vector2f(point.x, point.y));
 };

 curve.shape.setOrigin(curve.centerPoint.x, curve.centerPoint.y);
 curve.shape.setPosition(curve.centerPoint.x, curve.centerPoint.y);
 curve.curveNamePosition.x = curve.centerPoint.x;
 curve.curveNamePosition.y = curve.centerPoint.y;
 curve.curveName.setString("Curve");
 curve.curveName.setPosition(curve.centerPoint.x, curve.centerPoint.y);

 return curve;
}

int main(int argc, char **argv) {
 const unsigned short windowWidth = 800;
 const unsigned short windowHeight = 800;

 sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight), "Demo",
                         sf::Style::Default); // Default / None // Fullscreen
 string myfontFileName = "./media/Arial.ttf";
 sf::Font myFont;

 if (!myFont.loadFromFile(myfontFileName)) {
   cout << "Could not find the font " << myfontFileName << endl;
 }

 sf::Event myEvent;
 sf::Clock curveClock;
 // Initialization
 point centerPoint;
 centerPoint.x = 300;
 centerPoint.y = 300;
 double radius = 200;
 curve mt = computeCurve(centerPoint, radius);
 mt.shape.setOutlineColor(sf::Color::Red);
 mt.shape.setFillColor(sf::Color(40, 140, 10, 127));
 mt.shape.setOutlineThickness(1.f);
 mt.curveName.setFont(myFont);
 mt.curveName.setCharacterSize(20);
 mt.curveName.setFillColor(sf::Color::White);

 std::random_device randomDevice;
 std::mt19937 seed(randomDevice());

 while (window.isOpen()) {
   while (window.pollEvent(myEvent)) {
     if (myEvent.type == sf::Event::EventType::Closed) {
       window.close();
     }
   }

   window.clear();

   if (curveClock.getElapsedTime().asMilliseconds() > 200.0f) {
     std::uniform_int_distribution<std::mt19937::result_type> rDistribution(
         10, 300);
     double radius = rDistribution(seed);

     std::uniform_int_distribution<std::mt19937::result_type> cDistribution(
         100, 300);
     centerPoint.x = cDistribution(seed);
     centerPoint.y = cDistribution(seed);

     mt = computeCurve(centerPoint, radius);
     mt.shape.setOutlineColor(sf::Color::Red);
     mt.shape.setFillColor(sf::Color(40, 140, 10, 127));
     mt.shape.setOutlineThickness(1.f);
     mt.curveName.setFont(myFont);
     mt.curveName.setCharacterSize(20);
     mt.curveName.setFillColor(sf::Color::White);
     curveClock.restart();
   }
   window.draw(mt.shape);
   window.draw(mt.curveName);
   window.display();
 }
 return EXIT_SUCCESS;
}
c++ memory-leaks valgrind sfml
1个回答
0
投票

定义方程式的方式肯定存在错误。如果有人感兴趣,这是一个更正的代码。

最诚挚的问候,

MC

#include <stdio.h>
#include <iostream>
#include <random>
#include <string>
#include <math.h>
#include "SFML/Graphics.hpp"

using namespace std;

struct point
    {
    double x;
    double y;
    };

struct curve
    {
    int index;
    point centerPoint;
    double radius;
    sf::ConvexShape shape;
    sf::Text name;
    point namePosition;
    };

curve computeCurve(point centerPoint,double radius)
    {
    std::random_device randomDevice;
    std::mt19937 seed(randomDevice());
    std::uniform_int_distribution<std::mt19937::result_type> aDistribution(1,4);
    int a=aDistribution(seed);

    unsigned short numberOfPoints=100*a;
    curve curve;
    curve.shape.setPointCount(numberOfPoints);
    curve.centerPoint.x=centerPoint.x;
    curve.centerPoint.y=centerPoint.y;
    curve.radius=radius;
    double alpha=2*M_PI/numberOfPoints;

    point point;
    for(unsigned short i=0;i<numberOfPoints+1;i++)
        {
        point.x=radius*(0.5+cos(2*a*alpha*i)/2)*cos(alpha*i)+centerPoint.x;
        point.y=radius*sin(a*alpha*i)+centerPoint.y;
        curve.shape.setPoint(i,sf::Vector2f(point.x,point.y));
        };

    curve.shape.setOrigin(curve.centerPoint.x,curve.centerPoint.y);
    curve.shape.setPosition(curve.centerPoint.x,curve.centerPoint.y);

    curve.namePosition.x=curve.centerPoint.x;
    curve.namePosition.y=curve.centerPoint.y;
    curve.name.setString("Curve");
    curve.name.setPosition(curve.centerPoint.x,curve.centerPoint.y);
    return curve;
    }   
curve computeCardioid(point centerPoint,double radius)
    {
    double perimeter=8*radius;
    int numberOfPoints=max(floor(perimeter/3),20.0d);
    curve curve;
    curve.shape.setPointCount(numberOfPoints);
    curve.centerPoint.x=centerPoint.x;
    curve.centerPoint.y=centerPoint.y;
    curve.radius=radius;
    double alpha=2*M_PI/numberOfPoints;
    point point;
    for(unsigned short i=0;i<numberOfPoints;i++)
        {
        point.x=2*radius*(1-cos(alpha*i))*cos(alpha*i)+curve.centerPoint.x;
        point.y=2*radius*(1-cos(alpha*i))*sin(alpha*i)+curve.centerPoint.y;
        curve.shape.setPoint(i,sf::Vector2f(point.x,point.y));
        };
    curve.shape.setOrigin(curve.centerPoint.x-1.5*radius,curve.centerPoint.y);
    curve.shape.setPosition(curve.centerPoint.x,curve.centerPoint.y);

    curve.namePosition.x=curve.centerPoint.x;
    curve.namePosition.y=curve.centerPoint.y;
    curve.name.setString("Card");
    curve.name.setPosition(curve.centerPoint.x,curve.centerPoint.y);

    return curve;
    }
int main(int argc, char** argv) 
    {
    const   unsigned short windowWidth = 800;
    const   unsigned short windowHeight = 800;

    sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight), "Demo",sf::Style::Default); // Default / None // Fullscreen
    string myfontFileName="./media/Arial.ttf";
    sf::Font myFont;
    if (!myFont.loadFromFile(myfontFileName))
        {
        cout << "Could not find the font " << myfontFileName << endl;
        }
    sf::Event myEvent;
    sf::Clock curveClock;
    // Initialization
    point centerPoint;
    centerPoint.x=300;
    centerPoint.y=300;
    double radius=200;

    curve curve=computeCurve(centerPoint,radius);
    curve.shape.setOutlineColor(sf::Color::Red);
    curve.shape.setFillColor(sf::Color(40,140,10,127));
    curve.shape.setOutlineThickness(1.f);
    curve.name.setFont(myFont);
    curve.name.setCharacterSize(20);
    curve.name.setFillColor(sf::Color::White);

    random_device randomDevice;
    mt19937 seed(randomDevice());

    while (window.isOpen())
        {
        while (window.pollEvent(myEvent))
            {
            if (myEvent.type == sf::Event::EventType::Closed)
                {
                window.close();
                }
            }
        window.clear();
        if (curveClock.getElapsedTime().asMilliseconds() > 1000.0f)
            {
            uniform_int_distribution<mt19937::result_type> rDistribution(10,300);
            double radius=rDistribution(seed);

            point centerPoint;
            uniform_int_distribution<mt19937::result_type> cDistribution(100,300);
            centerPoint.x=cDistribution(seed);
            centerPoint.y=cDistribution(seed);
            curve=computeCurve(centerPoint,radius);
            curve.shape.setOutlineColor(sf::Color::Red);
            curve.shape.setFillColor(sf::Color(40,140,10,127));
            curve.shape.setOutlineThickness(1.f);
            curve.name.setFont(myFont);
            curve.name.setCharacterSize(20);
            curve.name.setFillColor(sf::Color::White);
            curveClock.restart();
            }
        window.draw(curve.shape);
        window.draw(curve.name);
        window.display();
        }
    return EXIT_SUCCESS;
    }```
© www.soinside.com 2019 - 2024. All rights reserved.