C++返回来自另一个函数的指针是复制它?瓦尔格林德抱怨道
C++ returning a pointer coming out of another function is copying it ? valgrind complains
很抱歉发布这个,但我还没有找到任何关于我在阅读瓦尔格林德结果时的感觉的讨论。我看到的是大型代码的一部分,所以我试图尽可能地简化它。我有一个类(类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
只是调用析构函数(如果已定义)。
您可以阅读此内容以获取更多信息 删除调用析构函数但不删除对象?
相关文章:
- 瓦尔格林德:数学函数"Conditional jump or move depends on uninitialised value(s)"
- 为什么瓦尔格林德在不释放恶意内存后没有报告任何问题?
- 瓦尔格林德的内存泄漏使用新的
- CPP 中的瓦尔格林德和记忆泄漏:"Conditional jump or move depends on uninitialised values"
- C++程序什么都不做,但瓦尔格林德显示内存分配
- 赫尔格林德和atomic_flag
- 删除堆后对瓦尔格林德错误感到困惑
- 为什么我会"Invalid read of size 8"?(瓦尔格林德)
- 瓦尔格林德报告在 =带有嵌套shared_ptrs的运算符上的读取错误
- 瓦尔格林德:注意到但未处理的国际奥克特尔;是否有必要处理以及如何找到?
- 瓦尔格林德错误 - 地址0x0不是堆叠的 malloc'd 或自由的
- 瓦尔格林德错误来自 boost::asio
- 瓦尔格林德可能丢失了,仍然可以到达并被瓦尔格林德检测到并被抑制
- 在瓦尔格林德的泄漏摘要中使用 std::ios::sync_with_stdio(fasle) 打印时获取"still reachable"
- 瓦尔格林德"不支持arch_prctl选项"
- 有没有办法只从瓦尔格林德获得泄漏摘要?
- 瓦尔格林德 + --泄漏检查 -> 不泄漏摘要
- 我找不到瓦尔格林德告诉我的记忆链接
- C++返回来自另一个函数的指针是复制它?瓦尔格林德抱怨道
- 瓦尔格林德未显示数组复制错误