Threads modifing the freed heap
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Threads modifing the freed heap

  1. #1
    Join Date
    Jul 2010
    Posts
    6

    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

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    1,998

    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

  3. #3
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Threads modifing the freed heap

    Quote Originally Posted by Mikan View Post
    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center