关于类静态成员变量指针通过动态分配的内存如何回收的探讨

来源:互联网 发布:无线路由器网络模式 编辑:程序博客网 时间:2024/05/23 01:32

一个类假如存在一个静态成员变量指针,在以下几种情况下动态分配内存,该如何回收内存:

1)在外部函数中动态分配内存,代码如下:

test.cpp

class Test{public:    static char* m_pSZ;};char* Test::m_pSZ = NULL;void testAlloc(){    Test::m_pSZ = new char[16];}int main(){    testAlloc();            std::cout << "Already alloc for Test::m_pSZ!" << std::endl;    return 0;}

通过使用g++ -g test.cpp -c test编译,再g++ -o test test.o链接成执行文件,最后通过检测内存泄漏工具valgrind检测,命令如下:

valgrind --tool=memcheck --leak-check=full --show-leak-kinds=all ./test

检测结果如下:

==14924== Memcheck, a memory error detector
==14924== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==14924== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==14924== Command: ./test
==14924== 
Already alloc for Test::m_pSZ!
==14924== 
==14924== HEAP SUMMARY:
==14924==     in use at exit: 16 bytes in 1 blocks
==14924==   total heap usage: 1 allocs, 0 frees, 16 bytes allocated
==14924== 
==14924== 16 bytes in 1 blocks are still reachable in loss record 1 of 1
==14924==    at 0x402ACDB: operator new[](unsigned int) (vg_replace_malloc.c:383)
==14924==    by 0x80486EE: testAlloc() (test.cpp:13)
==14924==    by 0x8048703: main (test.cpp:18)
==14924== 
==14924== LEAK SUMMARY:
==14924==    definitely lost: 0 bytes in 0 blocks
==14924==    indirectly lost: 0 bytes in 0 blocks
==14924==      possibly lost: 0 bytes in 0 blocks
==14924==    still reachable: 16 bytes in 1 blocks
==14924==         suppressed: 0 bytes in 0 blocks
==14924== 
==14924== For counts of detected and suppressed errors, rerun with: -v
==14924== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

从上面结果看,还是有泄露的,泄露类型属于"still reachable",对于这类型泄露,在网上查了下:

内存泄露可以分为两种:
一种是,程序中有指针指向通过malloc或者new申请的内存,但是在程序结束前,一直未收回。如果这种内存一直增加的话,可能导致内存耗尽。不过程序结束后系统会自动回收这些内存。
另一种是,通过malloc或者new申请的内存,但是程序中已经没有指针指向申请的内存。程序一直在执行,泄露的内存会越来越多,可能会耗尽程序的堆内存。

对于上述的第一种内存泄露的情况,在valgrind的报告中会表示为“still reachable”。
对于第二种情况的内存泄露,在valgrind的报告中会表示为”Directly lost和Indirectly lost”
另外对于valgrind报告中的“possibly lost”,是因为指针没有指向到申请堆的开始。如,c++的string有内存池的概念。如果程序中使用了string,且非正常退出(如使用ctrl+c快捷键终止程序),会报“possibly lost”。

从上面分析可知,“still reachable”类型内存泄露,对内存并不影响,程序结束后,会自动回收它们。但也可以显示的回收该类静态成员指针内存,直接在main中delete [] Test::m_pSZ即可。

2)在类的静态成员函数中给静态成员变量动态分配了内存,应该在哪些地方显示的回收这些内存呢?在静态成员函数中吗?还是任何地方?在一篇博文中博主说:“在类的生命周期中,类的静态成员不能被delete”.如果在类的生命周期中,delete类的静态成员,会core,下面用如下代码测试:

test_3.cpp

#include <iostream>class B{private:    static char* m_pSZ;public:    inline B()    {        if(NULL == m_pSZ)        {            m_pSZ = new char[8];        }    }    inline ~B()    {        if(m_pSZ != NULL)        {            delete []m_pSZ;        }    }};char* B::m_pSZ = NULL;int main(){    B b;    std::cout << "正处于类的生命周期中!" << std::endl;    return 0;}
上面代码中,m_pSZ肯定和类的生命周期一样,而对象b的生命周期肯定比m_pSZ短,但并不妨碍,在m_pSZ生命周期之前释放内存,m_pSZ指针是静态变量,在内存的全局区域,而该指针指向的内存是堆内存,在可以获得指向堆中某段内存的指针情况下,为啥不能释放,不合规矩。且在实践中,经过简单的编译,运行,并没有出现dump core现象。

使用valgrind工具检测结果如下:

valgrind --tool=memcheck --leak-check=full --show-leak-kinds=all ./test_3
==15418== Memcheck, a memory error detector
==15418== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==15418== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==15418== Command: ./test_3
==15418== 
正处于类的生命周期中!
==15418== 
==15418== HEAP SUMMARY:
==15418==     in use at exit: 0 bytes in 0 blocks
==15418==   total heap usage: 1 allocs, 1 frees, 8 bytes allocated
==15418== 
==15418== All heap blocks were freed -- no leaks are possible
==15418== 
==15418== For counts of detected and suppressed errors, rerun with: -v
==15418== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

所以,对于类的静态成员变量指针动态分配内存,可以在程序结束前的任何能够访问静态成员变量指针的地方,对动态分配内存进行释放,也可以不显示释放,因为程序结束后,系统会自动回收。

最后,对于test_3.cpp代码,在类的构造函数中动态分配内存,等于多个对象共享该静态变量指针,因此若某个对象析构了,释放了内存,其他对象若还使用该共享的静态变量指针,就是访问空指针啦!,故更好的设计是,使用静态变量维护一个引用计数,每创建一个对象,引用计数加1,析构函数中,引用计数减一,直到为0才释放内存。代码如下:

#include <iostream>class B{private:    static char* m_pSZ;    static unsigned int m_count;public:    inline B()    {        if(NULL == m_pSZ)        {            m_pSZ = new char[8];         }        ++m_count;        std::cout << m_count << std::endl;    }    inline ~B()    {        if(m_pSZ != NULL)        {            --m_count;            if(0 == m_count)            {                delete []m_pSZ;                m_pSZ = NULL;            }        }        std::cout << m_count << std::endl;    }};char* B::m_pSZ = NULL;unsigned int B::m_count = 0;int main(){    std::cout << "正处于类的生命周期中!" << std::endl;    B b;    B b1;    B b2;    B b3;    return 0;}




0 0
原创粉丝点击