4

From this question, I implement like this

Utils.h:

template<class T>
void SafeDeleteWithoutClass(T* &pVal) {
    std::cout << __FUNCTION__ << std::endl;
    delete pVal;
    pVal = nullptr;
}

template<class T>
void SafeDeleteArrayWithoutClass(T* &pVal) {
    std::cout << __FUNCTION__ << std::endl;
    delete[] pVal;
    pVal = nullptr;
}

main.cpp

#include "Utils.h"
struct Foo {
    Foo() {std::cout << __FUNCTION__ << std::endl;}
    ~Foo() {std::cout << __FUNCTION__ << std::endl;}
}
int main()
{
    Foo *pObj = new Foo();
    SafeDeleteWithoutClass<Foo>(pObj2);
    return 0;
}

It works well. But some guys in my team said it is not really better than macro. I don't know whether any example to prove this way is better than macro. Could you give me a hint?

gmas80
  • 1,218
  • 1
  • 14
  • 44
GAVD
  • 1,977
  • 3
  • 22
  • 40
  • Possible duplicate of [Reason why not to have a DELETE macro for C++](https://stackoverflow.com/questions/1265666/reason-why-not-to-have-a-delete-macro-for-c) – gmas80 Jan 30 '18 at 04:24
  • Sure it's better than a macro. This is easier to debug, if need be, and imagine you have the same macro called `SAFE_DELETE`, and then someone writes `#define SAFE_DELETE(p) p =nullptr`, then what? – Tas Jan 30 '18 at 04:24
  • 2
    There's really no true "safe" delete. Even with your functions (or macros, and indeed your functions are not much better than the macros) it doesn't stop anyone from using the pointer after calling your `SafeDeleteWithoutClass` function, leading to a null-pointer dereference. It's only "safe" as long as one remember to check the pointers. Lastly, if one *really* need to use pointers, use [smart pointers](http://en.cppreference.com/w/cpp/memory#Smart_pointers), otherwise try to avoid them as much as possible (in favor of objects or `std::array` or `std::vector`, as applicable). – Some programmer dude Jan 30 '18 at 04:25
  • 1
    do not reinvent the wheel. Use `std::uniqule_ptr` or `std::make_unique`. – Marek R Jan 30 '18 at 04:30
  • https://stackoverflow.com/questions/1265666/reason-why-not-to-have-a-delete-macro-for-c is absolutely odd, obsolete article. The question was asked before new C++11 released. Use smart pointers and be happy. – 273K Jan 30 '18 at 04:31

1 Answers1

2

It is not better then macro because you have no guarantee that it will save you from using pointer after deletion and deleting all things. It work practically the same. I guess that these guys just think it not better because it is the same. It is just written without using macro that's all.

Only advantage here is that you print some information on std::cout (but you can still do this in macro). And there are many disadventages here, to name few:

  1. You can forget to call this function and have memory leak
  2. Some one can still use pointer after it was deleted
  3. It is hard to find if only one small object is leaking
  4. You can call wrong function (e.g delete array for regular pointer)

Better to use std::shared_ptr<T> and std::unique_ptr. They will keep managing memory and make it clear who is the owner of memory (and memory ownership is important thing to consider on design of a class or whole project). But remember that smart pointers are not some miracle thing, you can still mess up some things. Take a look at this article to find out what not to do with them Top 10 dumb mistakes to avoid with C++ 11 smart pointers

wdudzik
  • 1,264
  • 15
  • 24