Boost中Core模块的checked_delete用法

头文件

boost/core/checked_delete.hpp

作用

delete 对象指针,或 对象数组指针,如果对象是不完整定义类型或者空指针,则 编译期间 出错。

何为不完整类型定义,就是只有 类型前置声明,没有引入头文件。

举例

//base.h
#include <iostream>
using namespace std;

class Base
{
public:
    Base()
    {}
    virtual ~Base()    
    {
        cout<<"~Base()"<<endl;
    }
};

//drive.h
#include <iostream>
using namespace std;

class Drive : public Base
{
public:
    Drive()
    {}
    virtual ~Drive()    
    {
        cout<<"~Drive()"<<endl;
    }
};

//deleteobject.h
class Base;
class DeleteObject
{
public:
    DeleteObject()
    {}
    ~DeleteObject()    
    {
        
    }
    
    void deleteObj(Base* base);
    
};

//deleteobject.cpp
void DeleteObject::deleteObj(Base* base)
{
    //并不会 delete Base对象,Base并没有执行。
    //因为 deleteobject.cpp 并没有 引言 base.h
    delete base;
}

//main.cpp
#include "drive.h"
#include "deleteobject.h"

int main(int argc, char* [])
{
    Base* base = new Drive();
    DeleteObject deOb;
    
    //
    deOb.deleteObj(base);
    return 0;
}

为了 避免 以上情况 发生,就需要 使用 checked_delete

举例

删除 空指针

#include <boost/checked_delete.hpp>  // for checked_delete

//  This program demonstrates compiler errors when trying to delete an
//  incomplete type.

namespace
{
    class Incomplete;
}

int main()
{
    Incomplete * p = 0;
    boost::checked_delete(p);          // should cause compile time error
    return 0;
}   // main

删除 空指针数组

#include <boost/checked_delete.hpp>  // for checked_delete

//  This program demonstrates compiler errors when trying to delete an
//  incomplete type.

namespace
{
    class Incomplete;
}

int main()
{
    Incomplete * p = 0;
    boost::checked_array_delete(p);    // should cause compile time error
    return 0;
}   // main

正常删除

#include <boost/core/checked_delete.hpp>
#include <boost/core/lightweight_test.hpp>

struct X
{
    static int instances;

    X()
    {
        ++instances;
    }

    ~X()
    {
        --instances;
    }

private:

    X( X const & );
    X & operator=( X const & );
};

int X::instances = 0;

int main()
{
    BOOST_TEST( X::instances == 0 );

    {
        X * p = new X;

        BOOST_TEST( X::instances == 1 );

        boost::checked_delete( p );

        BOOST_TEST( X::instances == 0 );
    }

    {
        X * p = new X[ 3 ];

        BOOST_TEST( X::instances == 3 );

        boost::checked_array_delete( p );

        BOOST_TEST( X::instances == 0 );
    }

    return boost::report_errors();
}

源码

namespace boost
{

// verify that types are complete for increased safety

template<class T> inline void checked_delete(T * x)
{
    // intentionally complex - simplification causes regressions
    typedef char type_must_be_complete[ sizeof(T)? 1: -1 ];
    (void) sizeof(type_must_be_complete);
    delete x;
}

template<class T> inline void checked_array_delete(T * x)
{
    typedef char type_must_be_complete[ sizeof(T)? 1: -1 ];
    (void) sizeof(type_must_be_complete);
    delete [] x;
}

template<class T> struct checked_deleter
{
    typedef void result_type;
    typedef T * argument_type;

    void operator()(T * x) const
    {
        // boost:: disables ADL
        boost::checked_delete(x);
    }
};

template<class T> struct checked_array_deleter
{
    typedef void result_type;
    typedef T * argument_type;

    void operator()(T * x) const
    {
        boost::checked_array_delete(x);
    }
};

} // namespace boost

猜你喜欢

转载自blog.csdn.net/zhangxiong1985/article/details/84426577