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

C++ returning a pointer coming out of another function is copying it ? valgrind complains

对此 post 表示抱歉,但我没有找到任何讨论来处理我在阅读 valgrind 结果时的感受。我看到的是一个大代码的一部分,所以我试图将它简化为 possible.I 有一个 class (ClassA),它包含一个指向另一个 object 的指针class(B 类)。两个 classes 都有一个名为 getArr 的方法 returns a double*。 ClassA 中的那个基本上 returns 通过它的指针从 ClassB 调用那个。 根据 valgrind 告诉我的,我感觉 ClassA 并没有真正从它的 ClassB object 返回指针,但它可能会复制它......是正确的,如果是这样如何避免它?

这里是 header:

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)

提前致谢 杰布

编辑:如评论中所述,上述行为与 valgrind 3.10 和 3.11 以及使用 g++ 5.1、5.3 和 clang++ 一致...唯一提供 3 次分配和 3 次释放的编译器(所以完全没有问题) 是 g++ 4.8

@Sarthak Singh:删除 ClassA 析构函数中的 delete 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)

尝试删除 ClassB 的析构函数,因为 delete 如果定义了析构函数就会调用它。

你可以阅读这篇文章了解更多信息Delete calling destructor but not deleting object?

在我看来泄漏来自 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 时才会看到泄漏)

它要么是真正的泄漏,要么是一些相互作用b/w gcc-5.x 附带的 libstdc++ 版本和挂钩 valgrind 注入到受监视的进程中。

据我所知,您对此无能为力。