class Apple
{
public:
Apple func(int depth) const;
private:
int size = 0; // size of data array
double *data = nullptr;
}
Apple::func(int depth) const
{
Apple new_apple;
'''
some calculations
'''
if(depth>1)
{
return new_apple.func(depth-1);
}else
{
return new_apple;
}
}
在方法func()中,我创建一个新的Apple对象,并根据存储在原始Apple对象中的数据执行一些计算。终端发出
Segmentation fault (core dumped)
我相信内存有问题,因为我从valgrind那里收到以下消息。
==4787== HEAP SUMMARY:
==4787== in use at exit: 5,994 bytes in 63 blocks
==4787== total heap usage: 1,059 allocs, 996 frees, 146,564 bytes allocated
==4787==
==4787== LEAK SUMMARY:
==4787== definitely lost: 0 bytes in 0 blocks
==4787== indirectly lost: 0 bytes in 0 blocks
==4787== possibly lost: 0 bytes in 0 blocks
==4787== still reachable: 5,994 bytes in 63 blocks
==4787== of which reachable via heuristic:
==4787== newarray : 1,080 bytes in 3 blocks
==4787== suppressed: 0 bytes in 0 blocks
Valgrind跟踪Apple类的重载运算符=。只有一个成员(我在这里命名数据)涉及动态内存,所以我只在这里显示与它相关的部分。以下是operator =的实现方式:
Apple& Apple::operator=(const Apple &obj)
{
if(this!=&obj)
{
if(this->data)
{
delete[] this->data;
}
this->size = obj.size;
this->data = new double[this->size];
for(int i=0; i<this->size; i++)
{
this->data[i] = obj.data[i];
}
}
return *this
}
=============================================== ==========================
如果我不添加递归组件,方法func()工作正常。而且我也用valgrind检查过,计算部分的内存没有问题。
Apple::func(int depth) const
{
Apple new_apple;
'''
some calculations
'''
return new_apple; // works fine
}
即使我使用depth = 1调用func(int深度),也会发生相同的分段错误。
Apple::func(int depth) const
{
Apple new_apple;
'''
some calculations
'''
if(depth>1)
{
std::cout << 1 << std::endl;
return new_apple.func(depth-1);
}else
{
std::cout << 2 << std::endl;
return new_apple;
}
}
&#34; 1&#34;或&#34; 2&#34;当我打电话给func(1)时打印出来。
=============================================== ========================== 调试来自gdb的消息:
Program received signal SIGSEGV, Segmentation fault.
Apple::operator= (this=0x0, T=...) at src/xxxx.cpp:1011
1011 if(this->data)
=============================================== ========================== 更新 :(添加了导致错误的示例代码)
我添加了一个更接近真实代码结构的示例代码。
示例代码:
http://coliru.stacked-crooked.com/a/3f7e50e4ebabf717
#include <iostream>
using namespace std;
class Apple
{
public:
Apple();
~Apple();
Apple(const Apple &obj);
void alloc(int size);
private:
double *data = nullptr;
int size = 0;
};
/////////////////////////////////////////////
class Basket
{
public:
Basket();
~Basket();
Basket(const Basket &obj);
void alloc(int size1, int size2);
Basket func(int depth);
private:
Apple *data = nullptr;
int size;
};
//////////////////////////////////////////////
Apple::Apple(){}
Apple::~Apple()
{
if(this->data)
{
delete[] this->data;
}
}
Apple::Apple(const Apple &obj)
{
if(this->data)
{
delete[] this->data;
}
this->size = obj.size;
this->data = new double[this->size];
for(int i=0; i<this->size; i++)
{
this->data[i] = obj.data[i];
}
}
void Apple::alloc(int size)
{
this->size = size;
if(this->data)
{
delete[] this->data;
}
this->data = new double[size];
}
///////////////////////////////////////////////////////////////////////
Basket::Basket(){}
Basket::~Basket()
{
if(this->data)
{
delete[] this->data;
}
}
Basket::Basket(const Basket &obj)
{
this->size = obj.size;
// This is the missing piece
/*
if(this->data)
{
delete[] this->data;
}
this->data = new Apple[this->size];
*/
for(int i=0; i<this->size; i++)
{
this->data[i] = obj.data[i];
}
}
void Basket::alloc(int size1, int size2)
{
if(this->data)
{
delete[] this->data;
}
this->size = size1;
this->data = new Apple[this->size];
for(int i=0; i<size1; i++)
{
this->data[i].alloc(size2);
}
}
Basket Basket::func(int depth)
{
Basket new_basket;
new_basket.alloc(5,5);
if(depth>1)
{
cout << 1 << endl;
return new_basket.func(depth-1);
}else
{
cout << 2 << endl;
return new_basket;
}
}
int main()
{
Basket basket;
Basket new_basket = basket.func(1);
return 0;
}
答案 0 :(得分:1)
问题已解决。
错误是由类篮子的复制构造函数的不正确实现引起的。
我的原始复制构造函数:
Basket::Basket(const Basket &obj)
{
this->size = obj.size;
for(int i=0; i<this->size; i++)
{
this->data[i] = obj.data[i];
}
}
正确的复制构造函数:
Basket::Basket(const Basket &obj)
{
this->size = obj.size;
if(this->data)
{
delete[] this->data;
}
this->data = new Apple[this->size];
for(int i=0; i<this->size; i++)
{
this->data[i] = obj.data[i];
}
}
答案 1 :(得分:0)
使用地址消毒剂(教授如何钓鱼而不是给鱼):
$ clang++ -Wall -std=c++14 soApple.cpp -o soApple -fsanitize=address -g
$ ./soApple
1
2
ASAN:DEADLYSIGNAL
=================================================================
==25955==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x7fff6cdb1fd0 bp 0x7ffeea7d7e80 sp 0x7ffeea7d7e80 T0)
==25955==The signal is caused by a WRITE memory access.
==25955==Hint: address points to the zero page.
#0 0x7fff6cdb1fcf in _platform_memmove$VARIANT$Haswell (libsystem_platform.dylib:x86_64+0x4fcf)
#1 0x10547d58f in __asan_memcpy (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x4f58f)
#2 0x105428f4c in Basket::Basket(Basket const&) soApple.cpp:72
#3 0x105428f8c in Basket::Basket(Basket const&) soApple.cpp:68
#4 0x10542957d in Basket::func(int) soApple.cpp:99
#5 0x1054294e4 in Basket::func(int) soApple.cpp:95
#6 0x105429b52 in main soApple.cpp:105
#7 0x7fff6caa0014 in start (libdyld.dylib:x86_64+0x1014)
==25955==Register values:
rax = 0x0000000000000000 rbx = 0x000000010620e880 rcx = 0x00006040000004d0 rdx = 0x0000000000000004
rdi = 0x0000000000000000 rsi = 0x0000608000000128 rbp = 0x00007ffeea7d7e80 rsp = 0x00007ffeea7d7e80
r8 = 0x0000000500006040 r9 = 0x00000001064297f0 r10 = 0x000000010ec703e0 r11 = 0xffff9f7ffffffed8
r12 = 0x000000000000000c r13 = 0x0000000000000000 r14 = 0x0000608000000128 r15 = 0x0000000000000000
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (libsystem_platform.dylib:x86_64+0x4fcf) in _platform_memmove$VARIANT$Haswell
==25955==ABORTING
Abort trap: 6
检查soApple.cpp
中的第72行时,它指向:
Basket::Basket(const Basket &obj)
{
this->size = obj.size;
for(int i=0; i<this->size; i++)
{
this->data[i] = obj.data[i]; <-- here is a crash
}
}
基本上没有设置/分配this->data
。
解决问题的最佳方法是使用std::vector
,然后此模板将负责内存管理,您的代码将更简单,更易读和可靠。