dushkin
2011-07-04 18:55:29 UTC
Hi All,
(Sorry for bad english)
I am having some troubles with destroying threads (I guess...)
I will try to describe the architucture very generally, and if more
information will be needed, I will apply.
I have a dialog based application. When I press a button, I create a
socket and calls its Listen method.
Every time, a connection is accepted, I create a new thread containg
two major objects - Another (CAsyncSocket based) socket - which
receives packets from the connection source and pushes them into a
CStringArray, and a new thread which pops the next packet from this
CStringArray object and handles it.
Both connection thread and handler thread are created using
AfxBeginThread.
I also used CCrticalSection object to protect the strings array from
being accessed by both thread at the same time.
Now, this packets handler thread has a timer which schedules the
packet pop out and then handling.
There might be ofcourse several connection requests from several
sources.
Also The packet handler main job is actually send some response (1 or
more).
My problem is that sometimes the connection is being reset. The
question is what I do then? I am suppose to close the connection and
wait for a new one.
But then, after all the attempts I made according to my best
knowledge, I always got the application to crash on some invalid
pointer.
In general, whenever the socket OnClose message handler was called, I
first killed the handler thread timer, and then called AfxEndThread(0)
to kill the handler thread. Then I called Sockets' ShutDown() and
Close() methods. But then I was stucked with the question how do I
kill the connection thread? and where?
But the code continues to execute and the application crashes on
access violation or reading violation, as far as I understood on the
connection thread pointer (returned from AfxBeginThread), which is
usualy an abnormal pointer.
I tried to give the important details, but maybe you'ld need more.
Please tell me.
Many thanks!
(Sorry for bad english)
I am having some troubles with destroying threads (I guess...)
I will try to describe the architucture very generally, and if more
information will be needed, I will apply.
I have a dialog based application. When I press a button, I create a
socket and calls its Listen method.
Every time, a connection is accepted, I create a new thread containg
two major objects - Another (CAsyncSocket based) socket - which
receives packets from the connection source and pushes them into a
CStringArray, and a new thread which pops the next packet from this
CStringArray object and handles it.
Both connection thread and handler thread are created using
AfxBeginThread.
I also used CCrticalSection object to protect the strings array from
being accessed by both thread at the same time.
Now, this packets handler thread has a timer which schedules the
packet pop out and then handling.
There might be ofcourse several connection requests from several
sources.
Also The packet handler main job is actually send some response (1 or
more).
My problem is that sometimes the connection is being reset. The
question is what I do then? I am suppose to close the connection and
wait for a new one.
But then, after all the attempts I made according to my best
knowledge, I always got the application to crash on some invalid
pointer.
In general, whenever the socket OnClose message handler was called, I
first killed the handler thread timer, and then called AfxEndThread(0)
to kill the handler thread. Then I called Sockets' ShutDown() and
Close() methods. But then I was stucked with the question how do I
kill the connection thread? and where?
But the code continues to execute and the application crashes on
access violation or reading violation, as far as I understood on the
connection thread pointer (returned from AfxBeginThread), which is
usualy an abnormal pointer.
I tried to give the important details, but maybe you'ld need more.
Please tell me.
Many thanks!