开发者

How to delete the object that is create inside the local function in c++?

开发者 https://www.devze.com 2023-03-21 10:52 出处:网络
I will explain the scenario using example. void func() { 开发者_StackOverflowCLine* pObj = new CLine();

I will explain the scenario using example.

   void func()
   {
     开发者_StackOverflow     CLine* pObj = new CLine();
   }
   void func2()
   {
   }

How to delete the CLine object in func2()? Is it possible?

Thanks


It's not possible. As soon as func finishes you've lost all reference to pObj and leaked its memory.


There is probably no need to use a free-store allocated variable at all in this case. Just do:

void func()
{
       CLine obj;
}
void func2()
{
}

obj will be automatically destroyed at the end of the scope.

If the variable absolutely must be free-store allocated, then you can put it in an unique_ptr as follows:

void func()
{
       const std::unique_ptr<CLine> obj(new CLine());
}
void func2()
{
}

if you are using c++03 then just replace unique_ptr with auto_ptr.

Anyway, if you really must use the code exactly as it is in your example then there is a way to still delete the CLine object. You have to use overloaded operator new and operator delete. It would have to be something like:

#include <vector>
#include <new>
#include <algorithm>
struct CLine {
    //... Other members ...

    static std::vector<void*> pointerCache;

    void* operator new(std::size_t size) throw(std::bad_alloc) {
        while (true) {
            void* pointer(malloc(size));
            if (pointer) {
                try {
                    pointerCache.push_back(pointer);
                    return pointer;
                }
                catch (std::bad_alloc&) {
                    free(pointer);
                }
            }
            if (std::new_handler handler = std::set_new_handler(0)) {
                std::set_new_handler(handler);
                (*handler)();
            }
            else {
                throw std::bad_alloc();
            }
        }
    }

    void operator delete(void *p) throw() {
        pointerCache.erase(std::remove(pointerCache.begin(), pointerCache.end(), p),pointerCache.end());
        free(p);
    }

    void* operator new(std::size_t size, std::nothrow_t const&) throw() {
        try {
            return operator new(size);
        } catch (std::bad_alloc&) {
            return 0;
        }
    }
    //Compressed for space constraints.
    void operator delete(void *p, std::nothrow_t const&) throw(){
        operator delete(p);}
    void* operator new[](std::size_t size) throw(std::bad_alloc){
        return operator new(size);}
    void operator delete[](void *p) throw(){operator delete(p);}
    void* operator new[](std::size_t size, std::nothrow_t const& nothrow) throw(){
        return operator new(size, nothrow);}
    void operator delete[](void *p, std::nothrow_t const& nothrow) throw(){
        operator delete(p, nothrow);}
};
std::vector<void*> CLine::pointerCache;

And then your code could look like this:

void func()
{
    CLine* pObj = new CLine();
}
void func2()
{
    //The most recently added pointer in CLine::pointerCache is
    // the one that was added in func()
    delete static_cast<CLine*> (CLine::pointerCache.back());
}

int main()
{
    func();
    func2();
}    

But really, there is so much wrong with this code that you would be better just changing your design. Some of the problems include: the total confusion that you will cause to people reading your code, the lack of thread-safety, and the use of a global pointerCache object.


It IS possible if func() and func2() are methods of the same class, and pObj is a member variable of that class. I realize this is not how you structured your question, but this is one way to get the effect you are looking for

class MyClass
{
    CLine* pObj;

     void func()
     {
          pObj = new CLine();
     }
     void func2()
     {
         delete pObj;
     }
}


Yes you can.

void func()
{
     CLine* pObj = new CLine();
}

void func2(CLine* pObj)
{
     delete pObj;
}

if you pass CLine pointer to func2


No, it's not possible. Because the only copy of the pointer is lost when func goes out of scope, you no longer have a reference to the memory to use with operator delete.


You have to delete the object you created dynamically in a function inside the function itself. If you leave the function without deleting it, you cannot delete it and the memory is lost.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号