Click to See Complete Forum and Search --> : Threads modifing the freed heap


Mikan
August 23rd, 2010, 04:48 PM
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

D_Drmmr
August 24th, 2010, 06:31 AM
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 (http://www.boost.org/doc/libs/1_44_0/doc/html/thread.html) 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.

MrViggy
August 24th, 2010, 10:54 AM
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