C++返回来自另一个函数的指针是复制它?瓦尔格林德抱怨道

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

本文关键字:林德抱 复制 回来 返回 另一个 函数 指针 C++      更新时间:2023-10-16

很抱歉发布这个,但我还没有找到任何关于我在阅读瓦尔格林德结果时的感觉的讨论。我看到的是大型代码的一部分,所以我试图尽可能地简化它。我有一个类(类A),它包含一个指向另一个类(类B)的对象的指针。 这两个类都有一个名为 getArr 的方法,该方法返回双精度*。类 A 中的那个基本上通过其指针返回对来自 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 个分配和 3 个自由(所以完全没有问题)的编译器是 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)
在我看来

,泄漏来自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,你对此无能为力。

尝试删除 ClassB 的析构函数,因为delete只是调用析构函数(如果已定义)。

您可以阅读此内容以获取更多信息 删除调用析构函数但不删除对象?