.exe 0xC0000005访问冲突中的未处理异常 [英] Unhandled exception in .exe 0xC0000005 access violation

查看:110
本文介绍了.exe 0xC0000005访问冲突中的未处理异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好:

当我尝试设计线程池时,在线程工作完成后,我需要通知线程池做一些工作,例如将该线程从繁忙列表移到空闲列表,我在3个.h文件中设计了3个类: > 1. CThreadPoolBase,一个结束线程的接口,用于通知具体线程池进行某些工作:

Hi,all:

When I was trying to design a thread pool, after thread job done, I need to notify the pool to do some work,like moving this thread from busy list to idle list,I designed 3 classes in 3 .h files:
1. CThreadPoolBase,an interface for an ending thread to notify the concrete thread pool to do some work:

/*CThreadPoolBase.h*/
clase CThreadPoolBase
{
public:
    virtual ~CThreadPoolBase()=0{}
    virtual void OnThreadFinished(LPVOID thr)=0; //para thr was used to pass a Thread*
}




2. CThreadPool:修复作业的具体线程池:




2. CThreadPool: the concrete thread pool that fix the job:

/*CThreadPool.h  */
#include  "CThreadPoolBase.h"
class CThreadPool:public CThreadPoolBase
{
public:
    ...
    void   OnThreadFinished(LPVOID thr)
    {
      <pre lang="cs">CThread *pthr = static_cast<CThread*>(thr);
    if( pthr != NULL )
        MoveToIdleList(pthr); //move itself to idle list
    else
    {
        cout<<"\t***[ERR_OnThreadFinished], Received error thread notify."<<endl;
        return;
    }
    int noff = GetIdleNum()-GetInitNum();
    if (noff>0) //if too many idle threads, kill some
    {
        DeleteIdleThread(noff);
    }   



}

}

3.线程:线程处理的类:



}

}

3. Thread: class of thread handling:

#include  "CThreadPoolBase.h
class Thread:
{
public:
    Thread(CThreadPoolBase *pool=NULL):m_pool(pool){}
    ~Thread(){}
    ... 

BOOL Thread::OnTask()     
{
    ...// do some work here
    
    //notify the pool that ''this'' thread has finished its work
    m_pool->OnThreadFinished(this);	//Exception caught <big>sometimes</big>
    return TRUE;
}
private:
    CThreadPoolBase *m_pool;
}


我遇到了这个异常:
.exe 0xC0000005访问冲突中未处理的异常

25:m_pool-> OnThreadFinished(this);
0040205C mov esi,esp
0040205E mov ecx,dword ptr [ebp-4]
00402061 Push ecx
00402062 mov edx,dword ptr [ebp-4]
00402065 mov ecx,dword ptr [edx + 50h]
00402068 mov eax,dword ptr [ebp-4]
0040206B mov edx,dword ptr [eax + 50h]
0040206E mov eax,dword ptr [edx] //它在这里停止
00402070呼叫dword ptr [eax + 4]
00402073 cmp esi,esp
00402075致电__chkesp(0040ed50)


有人会告诉我,问题出在哪里,它是怎么发生的?
谢谢!


I got this exception:
Unhandled exception in .exe 0xC0000005 access violation

25: m_pool->OnThreadFinished(this);
0040205C mov esi,esp
0040205E mov ecx,dword ptr [ebp-4]
00402061 push ecx
00402062 mov edx,dword ptr [ebp-4]
00402065 mov ecx,dword ptr [edx+50h]
00402068 mov eax,dword ptr [ebp-4]
0040206B mov edx,dword ptr [eax+50h]
0040206E mov eax,dword ptr [edx] //it stopped here
00402070 call dword ptr [eax+4]
00402073 cmp esi,esp
00402075 call __chkesp (0040ed50)


Anybody would kindly tell me, what''s the problem, and how did it happen?
Thank you !

推荐答案

在调试器中单步执行代码时会看到什么?
What do you see when you step through your code in the debugger?


此调用是直接的调用线程管理器(非线程安全!):
This call is a direct call to the thread manager (non-thread safe!):
m_pool->OnThreadFinished(this);



它一定会引起问题,因为您实际上在其中DeleteIdleThread()(尝试删除调用该方法的线程).完成此操作并等待同步信号后,在MFC中执行此操作的正确方法是PostMessage().如果您不使用MFC,则只需查找非阻塞线程调用和/或同步方法,以了解您的框架碰巧是什么.



Its bound to cause problems because you actually DeleteIdleThread() within it (you try to delete the thread that called the method). The proper way of doing this within MFC would be to PostMessage() when you''re done and wait for a synchronization signal. If you''re not using MFC, just look up non-blocking thread calls and/or synchronization methods for whatever your framework happens to be.


我已经找到了问题,它'' s我的错,在函数CreateIdleThread中发现错误,该函数用于在必要时动态创建空闲线程:

void CThreadPool :: CreateIdleThread(const int num)
{
I have found the problem,It''s my fault,error was found in function CreateIdleThreadused to create idle threads dynamicly when necessary:

void CThreadPool::CreateIdleThread(const int num)
{
for (int i=0;i<num;i++)
    {
        Thread *thr = new Thread(/*here I forgot to pass pointer this */);       

        AppendToIdleList(thr);
        m_VarMutex.Lock();
        m_nAvailNum++;
        m_VarMutex.Unlock();
        printf("\t\tCreate %d Idle thread . All=%d, busy=%d,Idle=%d\n",num,m_ThreadList.size(),m_BusyList.size(),m_IdleList.size());

        printf("*** Thanks a lot and best regards to all of you !! ^|^ ***\n");
   }



}



}


这篇关于.exe 0xC0000005访问冲突中的未处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆