Where did variable = null as “object destroying” come from?

后端 未结 14 1875
无人共我
无人共我 2021-02-18 15:51

Working on a number of legacy systems written in various versions of .NET, across many different companies, I keep finding examples of the following pattern:

pub         


        
14条回答
  •  心在旅途
    2021-02-18 16:29

    It comes from C/C++ where explicitly made setting your pointers to null was the norm (to eliminate dangling pointers)

    After calling free():

    #include 
    {
        char *dp = malloc ( A_CONST );
    
        // Now that we're freeing dp, it is a dangling pointer because it's pointing
        // to freed memory
        free ( dp );
    
        // Set dp to NULL so it is no longer dangling
        dp = NULL;
    }
    

    Classic VB developers also did the same thing when writing their COM components to prevent memory leaks.

提交回复
热议问题