1. 指针内存泄漏

使用new方法创建对象后,如果不执行delete函数,这个对象会一直保留在HEAP上。导致内存泄漏。

代码清单 3.4.1 指针内存泄

首先定义一个简单的类

  1. #ifndef PERSON_H
  2. #define PERSON_H
  3. #include <iostream>
  4. class Person
  5. {
  6. public:
  7. Person();
  8. ~Person();
  9. void showinfo(void);
  10. };
  11. #endif
#include "person.h"
using namespace std;

Person::Person()
{
    cout << "Person()" << endl;
}
Person::~Person()
{
    cout << "~Person()" << endl;
}
void Person::showinfo(void)
{
    cout << "showinfo()" << endl;
}

然后在main函数中调用这个函数来创建这个类的指针

#include "person.h"
void test_func(void)
{
    Person *p = new Person();
    p->showinfo();
}
int main(int argc, char **argv)
{
    int i;
    for(i = 0; i < 2;i++)
    {
        test_func();
    }
    return 0;
}

运行结果

solen@ubuntu:~/cpp$ ./person Person() showinfo() Person() showinfo()

并没有调用析构函数,原因是使用new方法创建的对象,位于内存的HEAP区域,而HEAP区域是不会像STACK区域那样自动回收的

2. 通过SP类来防止内存泄漏

核心理念是,在SP类中创建一个指向目标class的指针成员。用这个指针的成员来获取new 创建的空间。这样类被销毁时,在析构过程中这个指针成员也就被销毁了。而new的对象是作为形参被传入。

#ifndef SP_H
#define SP_H

#include "person.h"

class sp
{
    Person *p;
public:
    sp();
    sp(Person * other);
    ~sp();
    Person * operator->(void);
};
#endif
#include "sp.h"
using namespace std;
sp::sp()
{

}
sp::sp(Person *other)
{
    cout << "sp person * other" << endl;
    p = other;
}
sp::~sp()
{
    cout << "~sp()" << endl;
    if(p)
        delete p;
}
Person * sp::operator->(void)
{
    return p;
}
#include "person.h"
#include "sp.h"
void test_func(void)
{
    //sp p = new Person();
    sp p(new Person());
    p->showinfo();
}
int main(int argc, char **argv)
{
    int i;
    for(i = 0; i < 2;i++)
    {
        test_func();
    }
    return 0;
}

运行结果

Person() sp person * other showinfo() ~sp() ~Person()\

3. 关于非Const引用的语义限制

临时变量不能作为非const引用参数,是因为C + + 编译器认为如果一个参数以非const引用传入,那么这个值是可以被修改的。由于临时变量的特殊性,用户不能操作临时变量,而且随时可能会被释放掉,所以一般来说,C++编译器加入了临时变量不能作为非const引用这个语义限制,限制这个非常规用法的潜在错误。

通俗一点解释就是: 非常量形式的变量,这个变量没有名字,并且直接丢弃了函数对这个变量的修改结果,这就导致编译器对这种行为非常困惑。因此直接禁用了这种形式。

例如我们在sp类增加一个引用形式的构造函数

#ifndef SP_H
#define SP_H
#include "person.h"
class sp
{
    Person *p;
public:
    sp();
    sp(Person * other);
    sp(sp &cpsp);
    ~sp();
    Person * operator->(void);
};
#endif

在main函数中直接调用

sp other = new Person();

会报编译错误

invalid initialization of non-const reference of type ‘sp&’ from an rvalue of type ‘sp’ sp p = new Person();

解决办法很简单就是前面增加 cosnt

4. 拷贝构造函数导致的意外释放

#include "person.h"
#include "sp.h"
using namespace std;
void test_func(void)
{
    sp p = new Person();
    p->showinfo();
}
void test_func(const sp &other)
{
    sp s = other; // = sp s(other);
    s->showinfo();
    cout << "test_func &" << endl;
}

int main(int argc, char **argv)
{
    int i;
    sp other = new Person();
    for(i = 0; i < 2;i++)
    {
        test_func(other);
    }
    return 0;
}

如上代码
main函数中创建 sp other 对象后,第一次执行test_func(const sp &other)函数后, 传入的引用被销毁,导致了sp other本身也被销毁,那么后面再次调用test_func时,传入的就是一个空对象,导致程序崩溃。

** Error in `./person’: double free or corruption (fasttop): 0x00000000006d4c20

解决方法是增加一个拷贝计数的静态成员变量。每调用一次拷贝构造函数计数值+1, 每调用一次析构函数计数值-1
直到为0时,才允许被调用,这与Java中的内存自动回收的机制是相同的。
修改后的sp类如下

#ifndef SP_H
#define SP_H

#include "person.h"

class sp
{
    Person *p;
    static int reference_cnt;
public:
    sp();
    sp(Person * other);
    sp(const sp &cpsp);
    ~sp();
    Person * operator->(void);
};
#endif
#include "sp.h"
using namespace std;
int sp::reference_cnt = 0;
sp::sp()
{

}
sp::sp(Person *other)
{
    cout << "sp person * other" << endl;
    p = other;
}
sp::sp(const sp &cpsp)
{
    cout << "sp copy construct" << endl;
    p = cpsp.p;
    reference_cnt++;
}
sp::~sp()
{
    cout << "~sp()" << endl;
    if((p) && (reference_cnt == 0))
    {
         delete p;
    }
    if(reference_cnt)
        reference_cnt--;
}
Person * sp::operator->(void)
{
    return p;
}

执行过程

solen@ubuntu:~/cpp$ ./person Person() sp person * other sp copy construct showinfo() test_func & ~sp() sp copy construct showinfo() test_func & ~sp() ~sp() ~Person()

最终是在main函数完成后,析构所有的对象

将sp类定义为类模板就可以处理各种各样的类

这个实际上就是Android LightRef 指针的原理