I am using 2D pointer matrix in C++ (using visual studio 2015). I created the matrix and deleted it after done.

```
typedef unsigned char  U8;
typedef unsigned int   U32;
U8** A0 = new U8*[M0];
for (U32 i = 0; i < M0; ++i)
{
    A0[i] = new U8[m_L0];
}
for (U32 i = 0; i < M0; ++i){
    if (i < m_L0)
    {
        delete[] A0[i];
        A0[i] = NULL;
    }
}
delete[] A0;
A0 = NULL;

```

However, I got a error as the bellow figure. Do I need to check matrix not NULL before delete? 
[![enter image description here](https://i.stack.imgur.com/0rdaG.png)](https://i.stack.imgur.com/0rdaG.png)

Sometime, it crashed as the figure
[![enter image description here](https://i.stack.imgur.com/gOn3q.png)](https://i.stack.imgur.com/gOn3q.png)

What is my happen? Could you help me to fix it? This is my full source code

```
#include <iostream>
#include <stdlib.h>
#include <time.h>       /* time */
#define random(x) (rand()%x)
typedef unsigned char  U8;
typedef unsigned int   U32;
class CData
{
private:
    U8* m_Data;
    U32  m_Len;
public:
    CData(void) : m_Data(NULL), m_Len(0)
    {
    }
    ~CData(void)
    {
        FreeData();
    }
    void FreeData()
    {
        if (m_Data)
        {
            delete[] m_Data;
            m_Data = NULL;
        }
    }
};
void create_and_delete_matrix()
{
    U32 M0;
    U32 m_L0;
    U32 range = 20;
    M0   = random(range);
    m_L0 = random(range);
    U8** A0 = new U8*[M0];
    for (U32 i = 0; i < M0; ++i)
    {
        A0[i] = new U8[m_L0];
    }
    for (U32 i = 0; i < M0; ++i) {
        for (U32 j = 0; j < m_L0; ++j)  {
            A0[i][j] = random(2);
            printf("%d ", A0[i][j]);
        }
        printf("\n");
    }
    CData** D0 = new CData*[M0];
    for (U32 i = 0; i < M0; ++i){
        D0[i] = new CData();
    }

    //free A0, C0, D0 matrix
    for (U32 i = 0; i < M0; ++i){
        if (i < m_L0)
        {
            delete[] A0[i];
            A0[i] = NULL;
        }
        delete D0[i];
        D0[i] = NULL;
    }
    delete[] A0;
    A0 = NULL;

    delete[] D0;
    D0 = NULL;

}
int main(int argc, char **argv) {
    unsigned int time_ui = static_cast<unsigned int>(time(NULL));
    srand(time_ui);
    for (U32 iter = 0; iter < 100; iter++){
        create_and_delete_matrix();
    }
    return 0;
}

```