为什么在“std :: ostream& operator<<”中我有记忆泄漏?

时间:2016-10-09 20:17:56

标签: c++ ostream

所以,我有一个简单的代码:

main.cpp中:

#include "source/core/Vector2D.h"
int main(int argc, char **argv)
{
   Vector2D vector;
   return 0;
}

Vector2D.h:

#ifndef VECTOR2D_H
#define VECTOR2D_H

// Стандартные библиотеки C++
#include <cmath>
#include <ostream>
class Vector2D
{
    public:
        Vector2D();
        ~Vector2D();
        friend std::ostream &operator<<(std::ostream& os, const Vector2D &v);
    protected:
        double m_x;
        double m_y;
    private:
};
#endif // VECTOR2D_H

和Vector2D.cpp

#include "Vector2D.h"

Vector2D::Vector2D()
{
    m_x = m_y = 0;
}

Vector2D::~Vector2D()
{
    //dtor
}

std::ostream &operator<<(std::ostream& os, const Vector2D &v)
{
    os<<'{'<<v.m_x<<';'<<v.m_y<<'}';
    os.flush();
    return os;
}

如果我用valgrind检查这段代码,那么:

valgrind --leak-check=full --show-leak-kinds=all  ./Game
==9886== Memcheck, a memory error detector
==9886== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==9886== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==9886== Command: ./Game
==9886== 
==9886== 
==9886== HEAP SUMMARY:
==9886==     in use at exit: 72,704 bytes in 1 blocks
==9886==   total heap usage: 1 allocs, 0 frees, 72,704 bytes allocated
==9886== 
==9886== 72,704 bytes in 1 blocks are still reachable in loss record 1 of 1
==9886==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9886==    by 0x4EC3EFF: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==9886==    by 0x40104E9: call_init.part.0 (dl-init.c:72)
==9886==    by 0x40105FA: call_init (dl-init.c:30)
==9886==    by 0x40105FA: _dl_init (dl-init.c:120)
==9886==    by 0x4000CF9: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==9886== 
==9886== LEAK SUMMARY:
==9886==    definitely lost: 0 bytes in 0 blocks
==9886==    indirectly lost: 0 bytes in 0 blocks
==9886==      possibly lost: 0 bytes in 0 blocks
==9886==    still reachable: 72,704 bytes in 1 blocks
==9886==         suppressed: 0 bytes in 0 blocks
==9886== 
==9886== For counts of detected and suppressed errors, rerun with: -v
==9886== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

72千字节的什么?我的代码有什么问题,或者是某些编译器或std库错误? 编译器标志:

-std=c++1y -I/usr/include/SDL2 -I"/home/maxim/workspace/Game/cppadv_game" -O0 -g3 -pedantic -pedantic-errors -Wall -Werror -c -fmessage-length=0

UPD: 这是一些std库bug:

maxim@MaximPC:echo "#include <iostream> 
> int main() {return 0;} " > prog.cpp
maxim@MaximPC:~/workspace/hello_world/Debug$ g++ prog.cpp -o prog
maxim@MaximPC:~/workspace/hello_world/Debug$ valgrind ./prog
==11116== Memcheck, a memory error detector
==11116== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==11116== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==11116== Command: ./prog
==11116== 
==11116== 
==11116== HEAP SUMMARY:
==11116==     in use at exit: 72,704 bytes in 1 blocks
==11116==   total heap usage: 1 allocs, 0 frees, 72,704 bytes allocated
==11116== 
==11116== LEAK SUMMARY:
==11116==    definitely lost: 0 bytes in 0 blocks
==11116==    indirectly lost: 0 bytes in 0 blocks
==11116==      possibly lost: 0 bytes in 0 blocks
==11116==    still reachable: 72,704 bytes in 1 blocks
==11116==         suppressed: 0 bytes in 0 blocks
==11116== Rerun with --leak-check=full to see details of leaked memory
==11116== 
==11116== For counts of detected and suppressed errors, rerun with: -v
==11116== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

1 个答案:

答案 0 :(得分:1)

这里没有错误:

p = subprocess.Popen("ping google.com", shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
for line in p.stdout:
    print(line)

它告诉你没有丢失的字节,只有72K的数据可以全局访问 - 由标准库分配的东西,并且在程序退出时仍然可以访问。标准库并不打算删除这些东西,因为这是浪费时间 - 当进程退出时内存会自动回收。

查看详细摘要,这看起来是用于在启动时加载动态库的内存(可能是libstc ++库)