Set pointers to nil after release?

前端 未结 4 816
臣服心动
臣服心动 2021-01-01 20:09

After releasing objects is it best to set the pointers to nil? Thats what I have been doing, just wanted to ask if its necessary, good practice or overkill?

         


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-01 20:17

    Usually when programming in C/C++ I set it to null. Why? Because even if you free the memory being pointed, the pointer still holds the address of that freed memory. It can cause a serious access violation problems in code like this:

    if(myPointer != null)
    {
       doSomething(myPointer);
    }
    

    If you had set your pointer to null, this will never happen

提交回复
热议问题