Threads modifing the freed heap
I'm testing my framework to work with threads, so I'm creating several threads, deleting them, creating again... and eventually I have this error:
Free Heap block XXXX modified at XXXX after it was freed.
I'm using the CWinThread class, and creating the threads in this way:
std::vector<CWorkerThread*> m_vThreads;
for (int nThreads = 0; nThreads<nNumThreads; nThreads++)
{
m_vThreads.push_back(new CWorkerThread(nThreads));
m_vThreads[nThreads]->CreateThread();
}
and deleting like this:
for (unsigned int nIndex = 0; nIndex<m_vThreads.size(); nIndex++)
{
delete m_vThreads[nIndex];
m_vThreads[nIndex] = NULL;
}
m_vThreads.clear();
CWorkerThread()
{
AfxBeginThread(Run, this, 0, NULL, CREATE_SUSPENDED, 0);
}
and in the destructor, I terminate the threads awakening them and returning 0.
Any suggestion of what can be the problem?
Thanks!
Mikan
Re: Threads modifing the freed heap
First, please post a minimal, but compilable example that reproduces your problem. Post the code inside code tags, such that we can read it.
Second, why are you trying to reinvent the wheel? Have a look at the Boost.Thread library. Most (if not all) of this will become part of C++0x using the same syntax. If you want to build a framework that will help you with multi-threading, I would base it on this.
Re: Threads modifing the freed heap
Quote:
Originally Posted by
Mikan
I'm testing my framework to work with threads, so I'm creating several threads, deleting them, creating again... and eventually I have this error:
Free Heap block XXXX modified at XXXX after it was freed.
I'm using the CWinThread class, and creating the threads in this way:
std::vector<CWorkerThread*> m_vThreads;
for (int nThreads = 0; nThreads<nNumThreads; nThreads++)
{
m_vThreads.push_back(new CWorkerThread(nThreads));
m_vThreads[nThreads]->CreateThread();
}
and deleting like this:
for (unsigned int nIndex = 0; nIndex<m_vThreads.size(); nIndex++)
{
delete m_vThreads[nIndex];
m_vThreads[nIndex] = NULL;
}
m_vThreads.clear();
CWorkerThread()
{
AfxBeginThread(Run, this, 0, NULL, CREATE_SUSPENDED, 0);
}
and in the destructor, I terminate the threads awakening them and returning 0.
Any suggestion of what can be the problem?
Thanks!
Mikan
What destructor? I don't see a class here...
Viggy