I have a multithread application, it uses threadpool so there are 10 threads running the same run() function as below:
run(){
...
SetTileAt(p, tile);
...
ClearPointsNotIn(list);
...
}
void TileMatrix::ClearPointsNotIn(QList<Point>list)
{
    removals.clear();
    mutex.lock();
    foreach(Point p, matrix.keys())
    {
        if(!list.contains(p))
        {
            removals.append(p);
        }
    }
    mutex.unlock();
    foreach(Point p,removals)
    {
        Tile* t=TileAt(p);
        if(t!=0)
        {
            mutex.lock();
            delete t;
            t=0;
            matrix.remove(p);
            mutex.unlock();
        }
    }
    removals.clear();
}
void TileMatrix::SetTileAt(const Point &p, Tile* tile)
{
     mutex.lock();
     Tile* t=matrix.value(p,0);
     if(t!=0)
        delete t;
     matrix.insert(p,tile);
     mutex.unlock();
}
Tile* TileMatrix::TileAt(const Point &p)
{
 Tile* ret;
 mutex.lock();
 ret=matrix.value(p,0);
 mutex.unlock();
 return ret;
}
And when I run the application, it some times crashed at the delete t part, I have checked the t's value at that moment it seems though t!=0, but the pointed content is totally garbage. I naively guess this is a "delete a deleted pointer problem". But I am not quite sure how is this happening and how could I modify the code to prevent it? Note that the muetex in TileAt with the one in ClearPointsNotIn() could create a dead lock...
 
     
     
    