C ++返回另一个函数的指针是复制吗? valgrind抱怨

时间:2016-03-09 18:08:04

标签: c++ pointers valgrind

很抱歉发布这个,但我没有找到任何讨论处理我在阅读valgrind结果时的感受。我所看到的是一个大代码的一部分,所以我试图尽可能地简化它。我有一个类(ClassA),它包含一个指向另一个类(ClassB)的对象的指针。这两个类都有一个名为getArr的方法,它返回一个double *。 ClassA中的那个基本上通过其指针返回对ClassB中的一个的调用。 从valgrind告诉我的情况来看,我觉得ClassA并没有真正从它的ClassB对象返回指针,但它可能会复制它......是否正确,如果是这样,如何避免它?

这是标题:

class ClassB
{
 public:
   ClassB(){}
  ~ClassB(){}
  double *getArr();

};

class ClassA
{
 public:
  ClassA();
 ~ClassA();
 double *getArr();

  ClassB *myB;
 };

功能:

#include <iostream>
#include "OtherClasses.h"

using namespace std;

ClassA::ClassA()
{
  myB = new ClassB();
}

ClassA::~ClassA()
{
  if( myB )
delete myB;
}

double* ClassA::getArr()
{
  return (myB->getArr());
}

double* ClassB::getArr()
{
  double* arr = new double[10];
  for(unsigned int i=0; i<10; i++)
arr[i]=i;

  return arr;
}

int main(int argc, char *argv[])
{

  ClassA *myA = new ClassA();
  double* pouet = myA->getArr();

  for(unsigned int i=0; i<10; i++)
cout<<pouet[i]<<endl;

  delete[] pouet;
  if (myA)
delete myA;

}

以及显示的内容:

==2115== Memcheck, a memory error detector
==2115== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==2115== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==2115== Command: ./bin/main
==2115== 
0
1
2
3
4
5
6
7
8
9
==2115== 
==2115== HEAP SUMMARY:
==2115==     in use at exit: 72,704 bytes in 1 blocks
==2115==   total heap usage: 4 allocs, 3 frees, 72,793 bytes allocated
==2115== 
==2115== 72,704 bytes in 1 blocks are still reachable in loss record 1 of 1
==2115==    at 0x4A06C0F: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==2115==    by 0x350388A1EF: ??? (in /usr/lib64/libstdc++.so.6.0.21)
==2115==    by 0x34FD80F669: call_init.part.0 (in /usr/lib64/ld-2.21.so)
==2115==    by 0x34FD80F77A: _dl_init (in /usr/lib64/ld-2.21.so)
==2115==    by 0x34FD800CC9: ??? (in /usr/lib64/ld-2.21.so)
==2115== 
==2115== LEAK SUMMARY:
==2115==    definitely lost: 0 bytes in 0 blocks
==2115==    indirectly lost: 0 bytes in 0 blocks
==2115==      possibly lost: 0 bytes in 0 blocks
==2115==    still reachable: 72,704 bytes in 1 blocks
==2115==         suppressed: 0 bytes in 0 blocks
==2115== 
==2115== For counts of detected and suppressed errors, rerun with: -v
==2115== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
提前谢谢 JBB

编辑:如评论中所述,上述行为与valgrind 3.10和3.11一致,并使用g ++ 5.1,5.3和clang ++ ...唯一提供3个alloc和3个frees的编译器(所以没有问题)是g ++ 4.8

@Sarthak Singh:在ClassA析构函数中删除删除myB会在valgrind的输出中显示这些行

==23329== 1 bytes in 1 blocks are definitely lost in loss record 1 of 2
==23329==    at 0x4A07117: operator new(unsigned long) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==23329==    by 0x400A4C: ClassA::ClassA() (main.cpp:8)
==23329==    by 0x400B2E: main (main.cpp:34)

2 个答案:

答案 0 :(得分:0)

尝试删除ClassB的析构函数,因为CREATE PROCEDURE OntoNewVersion @new_title NVARCHAR(100) AS INSERT INTO Versions (title) VALUES (@new_title) SET @versid = SCOPE_IDENTITY() SET @qrows = SELECT COUNT(*) FROM Questions; -- this is wrong, I know SET @i = 1; WHILE @i <= @qrows BEGIN SET @thisq = SELECT * FROM Questions WHERE id=@i INSERT INTO Questions (qtext,subsection_id,version_id,viewtype) VALUES (@thisq.qtext,@thisq.subsection_id,@versid,@thisq.viewtype); END 只是调用析构函数(如果已定义)。

您可以阅读此内容以获取更多信息Delete calling destructor but not deleting object?

答案 1 :(得分:0)

在我看来泄漏来自libstdc ++:

$> valgrind --leak-check=full --show-leak-kinds=all ./a.out
[...]
==6664== 72,704 bytes in 1 blocks are still reachable in loss record 1 of 1
==6664==    at 0x4C2ABD0: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6664==    by 0x4EBFE7F: pool (eh_alloc.cc:117)
==6664==    by 0x4EBFE7F: __static_initialization_and_destruction_0 (eh_alloc.cc:244)
==6664==    by 0x4EBFE7F: _GLOBAL__sub_I_eh_alloc.cc (eh_alloc.cc:307)
==6664==    by 0x400F3B9: call_init.part.0 (in /usr/lib/ld-2.23.so)
==6664==    by 0x400F4CA: _dl_init (in /usr/lib/ld-2.23.so)
==6664==    by 0x4000DC9: ??? (in /usr/lib/ld-2.23.so)
[...]

(这解释了为什么只有在使用gcc-5.x时才能看到泄漏)

它是一个真正的泄漏或者是与gcc-5.x附带的libstdc ++版本之间的一些相互作用,并且钩子valgrind注入受监视的进程。

AFAIK,你无能为力。