Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using CSocket in thread causes crash?

131 views
Skip to first unread message

Yef

unread,
Mar 10, 2005, 10:22:09 PM3/10/05
to
Hi all,

I put this tiny snippet of code into a thread
that I'm starting with AfxBeginThread, and suddenly
the thread crashes the program.

CSocket sock;
if (!sock.Create (0, SOCK_STREAM, NULL)) {
return 1;
}

It bombs inside the call to Create. I believe the error
is illegal access to the wrong part of memory.

Does anybody know what I'm doing wrong?

Thanks.

Fenster

unread,
Mar 11, 2005, 2:13:31 AM3/11/05
to
In message <1110511329....@g14g2000cwa.googlegroups.com>, Yef
<e9...@yahoo.com> writes

I may be mistaken but I'm sure if read somewhere that AfxSocketInit()
has to be called in each thread that will use the socket libraries. You
may already be doing this but if you're not...

--
Fenster

Yef

unread,
Mar 11, 2005, 8:57:08 AM3/11/05
to
I wasn't calling it but adding that call doesn't change anything.
I still a crash with get this call stack:

CMapPtrToPtr::GetValueAt(void * 0x00000754) line 179 + 3 bytes
CAsyncSocket::LookupHandle(unsigned int 1876, int 0) line 385 + 15
bytes
CAsyncSocket::AttachHandle(unsigned int 1876, CAsyncSocket * 0x00de7d84
{CAsyncSocket}, int 0) line 407 + 13 bytes
CAsyncSocket::Socket(int 1, long 0, int 0, int 2) line 557
CAsyncSocket::Create(unsigned int 0, int 1, long 0, const char *
0x00000000) line 105 + 20 bytes
MyThread (void * 0x0011df80) line 543 + 19 bytes
_AfxThreadEntry(void * 0x0011debc) line 112 + 13 bytes
_threadstartex(void * 0x00aa10a0) line 212 + 13 bytes
KERNEL32! 7c80b50b()

Ashy

unread,
Mar 11, 2005, 9:10:51 AM3/11/05
to
Dear Yef,
Can u elaborate on the intention behind doing this inside a thread ?.Is
it that you want create seperate socket to service each client ?
It is only a suggestion, before you call the sock.create( ) try calling
WSAStartup, of Use AfxSocketInit()

Yef

unread,
Mar 11, 2005, 10:15:41 AM3/11/05
to
Hi Ashy,

I'm creating a multithreaded program where each
thread will be accessing services on the Internet.

My understanding is that I need to use a "UI thread" instead
of a worker thread for this, because those have message
pumps and for some reason that is needed for winsock.

However so far, I can't create a UI thread.

UI threads clearly require that I create a CWinThread-derived
class because the AfxBeginThread class requires that I pass it
a runtime class information struct.

However, when I try to derive anything from CWinThread,
coding by hand (no classwizard involved), it doesn't link.

Thanks for any help-

Scott McPhillips [MVP]

unread,
Mar 11, 2005, 6:34:53 PM3/11/05
to

Well, you need to work on "it doesn't link". The MFC socket classes
definitely need the "UI" thread and the version of AfxBeginThread that
requires a runtime class. The reason is that socket notifications, such
as OnReceive and OnSend, are generated by messages that winsock posts to
the thread's message queue. Only a "UI" thread provides this message queue.

--
Scott McPhillips [VC++ MVP]

Yef

unread,
Mar 12, 2005, 8:00:37 AM3/12/05
to

Scott McPhillips [MVP] wrote:

> The MFC socket classes
> definitely need the "UI" thread and the version of AfxBeginThread
that
> requires a runtime class.

I got that working, but here's a funny question: Why does MS
provide the method CreateThread if it doesn't work.

> The reason is that socket notifications, such
> as OnReceive and OnSend, are generated by messages that winsock posts
to
> the thread's message queue. Only a "UI" thread provides this message
queue.

Well, now I've got the thread running, however when it reaches
the socket creation is bombs, just as it did in the non-UI thread.
I'm still using :

CSocket sock;
if (!sock.Create (0, SOCK_STREAM, NULL)) {
return 1;
}

However I've tried CAsyncSocket; that doesn't work, and I've
tried adding the call to AfxSocketInit in the beginning of
the thread. That doesn't help either.

I'm amazed that something so simple could be so complicated.

Any help appreciated-

Scott McPhillips [MVP]

unread,
Mar 12, 2005, 10:39:02 AM3/12/05
to
Yef wrote:
> I got that working, but here's a funny question: Why does MS
> provide the method CreateThread if it doesn't work.

It does work. It is just not appropriate in most cases. Your program
is using two libraries (C runtime and MFC) that need to make provisions
for multithreading. There are rules established by the libraries.
AfxBeginThread takes care of all the rules.


> Well, now I've got the thread running, however when it reaches
> the socket creation is bombs, just as it did in the non-UI thread.
> I'm still using :
>
> CSocket sock;
> if (!sock.Create (0, SOCK_STREAM, NULL)) {
> return 1;
> }
>
> However I've tried CAsyncSocket; that doesn't work, and I've
> tried adding the call to AfxSocketInit in the beginning of
> the thread. That doesn't help either.
>
> I'm amazed that something so simple could be so complicated.
>
> Any help appreciated-

Well, clearly the problem is elsewhere than in the above code. Study
the Microsoft sample code in the link below. Part of your problem may
be that you think running a message-driven socket in a multithreaded MFC
app should be simple. It takes study and great care. To analyze the
problem you need to understand how the MFC socket classes and thread
classes work, and the requirements they impose. Based on my experience
with both I strongly recommend you use CAsyncSocket and not CSocket.

http://download.microsoft.com/download/vc60pro/Sample4/1/WIN98/EN-US/MultiSoc.exe

Yef

unread,
Mar 12, 2005, 2:04:08 PM3/12/05
to
Hi Scott,

Thanks for this info. You mentioned that I'm using a message-
driven socket. Just curious but is there such a thing as a
non-message driven socket in Windows?

Thanks.

Yef

unread,
Mar 12, 2005, 4:02:07 PM3/12/05
to

> Thanks for this info. You mentioned that I'm using a message-
> driven socket. Just curious but is there such a thing as a
> non-message driven socket in Windows?

Also, looking at the example you pointed out, I see that
the connection is formed is the parent app, at which point
the socket id is detached and passed to the thread.
Is there a way to create the thread and form the connection
from inside the thread?

Thanks.

Scott McPhillips [MVP]

unread,
Mar 12, 2005, 4:15:02 PM3/12/05
to

Yes, there are several ways. They are described in the winsock section
of the MSDN documentation.

Scott McPhillips [MVP]

unread,
Mar 12, 2005, 4:20:44 PM3/12/05
to
Yef wrote:
> Also, looking at the example you pointed out, I see that
> the connection is formed is the parent app, at which point
> the socket id is detached and passed to the thread.
> Is there a way to create the thread and form the connection
> from inside the thread?
>
> Thanks.

Yes, there's nothing different about creating a CAsyncSocket or CSocket
inside a UI thread. They work the same way there that they work in the
main thread.

Yef

unread,
Mar 12, 2005, 4:42:32 PM3/12/05
to

That sounds nice, but I'm seeing evidence to the contrary.
I wish I had a decent example to look at but everything
I've found so far has involved creating the CSocket in the
main app, connecting or listening in the main app, and then
handing a socket id to the thread.

Yef

unread,
Mar 12, 2005, 5:46:10 PM3/12/05
to

Scott McPhillips [MVP] wrote:

> Yes, there are several ways. They are described in the winsock
section
> of the MSDN documentation.

It took me maybe 10 minutes to write some socket code in the
traditional Unix fashion i.e. using Windows' SOCKET datatype
and connect(), which has compiled and run without problems.

Arkady Frenkel

unread,
Mar 18, 2005, 2:52:08 PM3/18/05
to
That because of thread unsafe nature of MFC and not Winsock
Arkady

"Yef" <e9...@yahoo.com> wrote in message
news:1110663752.0...@g14g2000cwa.googlegroups.com...

0 new messages