Deleting pointer sometimes results in heap corruption

前端 未结 7 655
深忆病人
深忆病人 2021-01-07 03:34

I have a multithreaded application that runs using a custom thread pool class. The threads all execute the same function, with different parameters.

These parameters

7条回答
  •  时光取名叫无心
    2021-01-07 04:23

    Let's turn this on its head: Why are you using pointers at all?

    class Params
    {
    int value1, value2; // etc...
    }
    
    class ThreadJob
    {
      int jobID;  // or whatever...
      Params params;
    }
    
    class ThreadPool
    {
      std::list jobs;
    
      void addJob(int job, const Params & p)
      {
         ThreadJob j(job, p);
         jobs.push_back(j);
      }
    }
    

    No new, delete or pointers... Obviously some of the implementation details may be cocked, but you get the overall picture.

提交回复
热议问题