August 23rd, 2010 04:48 PM
#1
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
August 24th, 2010 06:31 AM
#2
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.
Cheers, D Drmmr
Please put [code][/code]
tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
August 24th, 2010 10:54 AM
#3
Re: Threads modifing the freed heap
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
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width
Bookmarks