写入文件时内存使用量会随着时间的推移而增加

时间:2016-10-12 15:05:37

标签: c++ qt

我有一个基于Qt的小型C ++程序,这个程序只是将一个向量写入文件,直到我点击" STOP"按钮。

在Qt设计师中,我只需按下一个按钮,将文本更改为" STOP"并命名对象"按钮"。

问题是当我通过终端中的watch命令检查程序的内存使用情况时,我发现可用内存随着时间的推移而减少,单击该按钮不会影响它。如果我关闭应用程序,它只会进入初始状态。

test.pro:

#-------------------------------------------------
#
# Project created by QtCreator 2016-10-12T13:56:45
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

QMAKE_CXXFLAGS += -std=c++11
QMAKE_CXXFLAGS += -D_GLIBCXX_USE_CXX11_ABI=0
QMAKE_CXXFLAGS += -pthread

TARGET = Test
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp \
    writer.cpp

HEADERS  += mainwindow.h \
    writer.h

FORMS    += mainwindow.ui

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <writer.h>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_stopButton_clicked();

    void on_startButton_clicked();

private:
    Ui::MainWindow *ui;
    writer *w;
};

#endif // MAINWINDOW_H

writer.h:

#ifndef WRITER_H
#define WRITER_H

#include <iostream>
#include <fstream>
#include <iterator>
#include <string>
#include <locale>
#include <sstream>
#include <thread>
#include <atomic>
#include <vector>

#define COUNTER_FILENAME "data_counter.txt"
#define DATA_PREFIX "data_no"
#define DATA_EXTENSION ".csv"

class writer
{
public:
    // For labeling
    unsigned int label_id;
    bool label;
    // Start writing frame features in a file
    void write();
    // Stop writing
    void stopWriting();
    // Default demo constructor, sets up the frame thread which will process frame data in a seperate thread
    explicit writer();
    // Default demo destructor, flags that the frame thread should stop and waits for the frame thread to join
    ~writer();

protected:
    std::unique_ptr< std::thread > frameThread;
    std::atomic< bool > stopFrameThread;
    std::vector< float > frameFeatures;
    std::string data_filename;
};

#endif // WRITER_H

main.cpp:

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    w(new writer())
{
    ui->setupUi(this);


}

MainWindow::~MainWindow()
{
    delete w;
    delete ui;
}

void MainWindow::on_stopButton_clicked()
{
    w->stopWriting();
}

void MainWindow::on_startButton_clicked()
{
    w->write();
}

writer.cpp:

#include "writer.h"

writer::writer() :
    label_id( 0 ),
    label( false )
{
    stopFrameThread = false;
    // Resize the vector to put all frame features
    frameFeatures.resize( 1743, 0.0 );
}

writer::~writer()
{
    stopWriting();
}

void writer::write()
{
    // Setup the frame thread
    stopFrameThread = false;

    // Create a new thread to retrieve all features of each frame
    frameThread = std::unique_ptr< std::thread >( new std::thread( [ this ](){
        // Read from counter file to get counter value
        std::string counter;
        std::ifstream counter_in (COUNTER_FILENAME);
        if( counter_in.is_open() ){
            std::getline( counter_in, counter );
            counter_in.close();
        }else{
            counter = "0";
        }

        // Convert the counter into an integer
        int c;
        std::stringstream( counter ) >> c;

        // Compute data filename
        data_filename = DATA_PREFIX + counter + "_id" + applicant_id + DATA_EXTENSION;

        std::ofstream data_file( data_filename );

        if( data_file.is_open() ){
            // Start the main processing loop
            while( ! stopFrameThread ){
                for(int index = 0; index < 1743; index++){
                    frameFeatures[ index ] = index;
                }

                for(std::vector< float >::iterator it = frameFeatures.begin(); it != frameFeatures.end(); ++it){
                    data_file << " " + std::to_string( *it );
                }
                data_file << "\n";
            }

            // Close the data file
            data_file.close();

            // Write incremented counter value on counter file
            std::ofstream counter_out (COUNTER_FILENAME);
            if( counter_out.is_open() ){
                counter_out << ++c;
                counter_out.close();
            }else{
                std::cerr << "Unable to write on counter file\n";
            }
        }else{
            std::cerr << "Unable to open the data file\n";
        }
    } ) );
}

void writer::stopWriting()
{
    // Flag that the frame thread should stop
    stopFrameThread = true;

    // Wait for the frame thread to stop
    if( frameThread ){
        if( frameThread->joinable() ){
            frameThread->join();
        }
    }
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我解决了这个问题已经有一段时间但是我认为我应该在这里写一下,以防有人遇到类似的问题。

代码中没有任何错误,也没有任何内存泄漏。该计划完美无缺。问题是创建新向量的过程比写入文件的过程快。

我通过两个独立的线程来解决它,以加快进程。第一个线程将创建向量并将它们存储在缓冲区中。第二个线程将从该缓冲区中获取一个向量并写入它。

相关问题