Stack Overflow exeception
-
Im getting a stack over flow execption its not from an ASSERT but storage on my stack frame is corrupted. I have a thread Waitting on event to be notified when a socket read is pending I trace socket number upon entry and its ok I initialize a WSABUF which 15 entires and its fine somewhere when I do the WaitforSingleObject the stack frame gets corrupted I did a data break point on the socket and its was somewhere in UserCallwinprocccheckwow. best to post my code some where in the WaiforSingleObject sonething happens to my stack frame here is the code for notification int ret1 = WSAEventSelect(mysocket, socksevent, FD_READ | FD_CLOSE); here is the createthread struct threadparm thethreads; thethreads.thesocket = mysocket; thethreads.sendwidow = pFrame->m_hWnd; thethreads.messge = WM_STORAGE; thethreads.sockevent = socksevent; struct threadparm* parmptr = &thethreads; HANDLE threadhandle = ::CreateThread(NULL, 0, SocketThread, (LPVOID)parmptr, 0, &threadid); the thread with Waitforsingleobect were somewhere threadptr->thesocket gets corrupted DWORD WINAPI SocketThread(LPVOID lphadleparater) { WSANETWORKEVENTS socknetwork; struct threadparm* threadptr; threadptr = (threadparm *)lphadleparater; WSABUF DataBuf[15]; DWORD recived; int i, j; DWORD flags = 0; WSAOVERLAPPED myoverlap; char* sendcopy = new char[3825]; char* holdptr = sendcopy; j = 0; for (i = 0; i < 15; i++) { DataBuf[i].buf = new char[255]; DataBuf[i].len = 255; } struct _WSABUF* tcpip = &DataBuf[0]; DWORD dwWaitStatus = WaitForSingleObject(threadptr->sockevent, INFINITE);
switch (dwWaitStatus)
/* a read is pending */
case WAIT_OBJECT_0 + 0:
{
int return_code = WSAEnumNetworkEvents(threadptr->thesocket, threadptr->sockevent, &socknetwork); -
Im getting a stack over flow execption its not from an ASSERT but storage on my stack frame is corrupted. I have a thread Waitting on event to be notified when a socket read is pending I trace socket number upon entry and its ok I initialize a WSABUF which 15 entires and its fine somewhere when I do the WaitforSingleObject the stack frame gets corrupted I did a data break point on the socket and its was somewhere in UserCallwinprocccheckwow. best to post my code some where in the WaiforSingleObject sonething happens to my stack frame here is the code for notification int ret1 = WSAEventSelect(mysocket, socksevent, FD_READ | FD_CLOSE); here is the createthread struct threadparm thethreads; thethreads.thesocket = mysocket; thethreads.sendwidow = pFrame->m_hWnd; thethreads.messge = WM_STORAGE; thethreads.sockevent = socksevent; struct threadparm* parmptr = &thethreads; HANDLE threadhandle = ::CreateThread(NULL, 0, SocketThread, (LPVOID)parmptr, 0, &threadid); the thread with Waitforsingleobect were somewhere threadptr->thesocket gets corrupted DWORD WINAPI SocketThread(LPVOID lphadleparater) { WSANETWORKEVENTS socknetwork; struct threadparm* threadptr; threadptr = (threadparm *)lphadleparater; WSABUF DataBuf[15]; DWORD recived; int i, j; DWORD flags = 0; WSAOVERLAPPED myoverlap; char* sendcopy = new char[3825]; char* holdptr = sendcopy; j = 0; for (i = 0; i < 15; i++) { DataBuf[i].buf = new char[255]; DataBuf[i].len = 255; } struct _WSABUF* tcpip = &DataBuf[0]; DWORD dwWaitStatus = WaitForSingleObject(threadptr->sockevent, INFINITE);
switch (dwWaitStatus)
/* a read is pending */
case WAIT_OBJECT_0 + 0:
{
int return_code = WSAEnumNetworkEvents(threadptr->thesocket, threadptr->sockevent, &socknetwork);It would be easier to look at your code if it was formatted. However, it isn't obvious to me what's causing your stack overflow. Do you have more code that you're not showing? A stack overflow is caused by too much function call nesting (perhaps because of recursion), too many local variables (usually large arrays), or creating a thread with a smaller stack than it needs (that's the second parameter to
CreateThread
, and since you're using0
, you're getting Windows' default size, which should be OK).Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing. -
Im getting a stack over flow execption its not from an ASSERT but storage on my stack frame is corrupted. I have a thread Waitting on event to be notified when a socket read is pending I trace socket number upon entry and its ok I initialize a WSABUF which 15 entires and its fine somewhere when I do the WaitforSingleObject the stack frame gets corrupted I did a data break point on the socket and its was somewhere in UserCallwinprocccheckwow. best to post my code some where in the WaiforSingleObject sonething happens to my stack frame here is the code for notification int ret1 = WSAEventSelect(mysocket, socksevent, FD_READ | FD_CLOSE); here is the createthread struct threadparm thethreads; thethreads.thesocket = mysocket; thethreads.sendwidow = pFrame->m_hWnd; thethreads.messge = WM_STORAGE; thethreads.sockevent = socksevent; struct threadparm* parmptr = &thethreads; HANDLE threadhandle = ::CreateThread(NULL, 0, SocketThread, (LPVOID)parmptr, 0, &threadid); the thread with Waitforsingleobect were somewhere threadptr->thesocket gets corrupted DWORD WINAPI SocketThread(LPVOID lphadleparater) { WSANETWORKEVENTS socknetwork; struct threadparm* threadptr; threadptr = (threadparm *)lphadleparater; WSABUF DataBuf[15]; DWORD recived; int i, j; DWORD flags = 0; WSAOVERLAPPED myoverlap; char* sendcopy = new char[3825]; char* holdptr = sendcopy; j = 0; for (i = 0; i < 15; i++) { DataBuf[i].buf = new char[255]; DataBuf[i].len = 255; } struct _WSABUF* tcpip = &DataBuf[0]; DWORD dwWaitStatus = WaitForSingleObject(threadptr->sockevent, INFINITE);
switch (dwWaitStatus)
/* a read is pending */
case WAIT_OBJECT_0 + 0:
{
int return_code = WSAEnumNetworkEvents(threadptr->thesocket, threadptr->sockevent, &socknetwork);Seems to me the
thethreads
was allocated on the stack and disappeared by the time thread is executing. As Greg was saying, formatting would be nice.Mircea
-
It would be easier to look at your code if it was formatted. However, it isn't obvious to me what's causing your stack overflow. Do you have more code that you're not showing? A stack overflow is caused by too much function call nesting (perhaps because of recursion), too many local variables (usually large arrays), or creating a thread with a smaller stack than it needs (that's the second parameter to
CreateThread
, and since you're using0
, you're getting Windows' default size, which should be OK).Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing. -
Seems to me the
thethreads
was allocated on the stack and disappeared by the time thread is executing. As Greg was saying, formatting would be nice.Mircea
-
is there a way to determine how much storage my local variables are currently taking maybe an output on the build thanks
If there's a compiler option to generate a listing file, that might contain the information. I've never done this in Windows C++, so you'd have to look into it. Another way would be to do this at the top of your function:
int first;
// rest of local variables
int last;int size = &last - &first; // distance from first to last local variable
if(size < 0) size = -size; // depends on how compiler lays out memory
size -= sizeof(int); // the real size after removing FIRST and LASTcout << "This function's locals use " << size << " bytes." << std::endl;
Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing. -
If there's a compiler option to generate a listing file, that might contain the information. I've never done this in Windows C++, so you'd have to look into it. Another way would be to do this at the top of your function:
int first;
// rest of local variables
int last;int size = &last - &first; // distance from first to last local variable
if(size < 0) size = -size; // depends on how compiler lays out memory
size -= sizeof(int); // the real size after removing FIRST and LASTcout << "This function's locals use " << size << " bytes." << std::endl;
Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing. -
If there's a compiler option to generate a listing file, that might contain the information. I've never done this in Windows C++, so you'd have to look into it. Another way would be to do this at the top of your function:
int first;
// rest of local variables
int last;int size = &last - &first; // distance from first to last local variable
if(size < 0) size = -size; // depends on how compiler lays out memory
size -= sizeof(int); // the real size after removing FIRST and LASTcout << "This function's locals use " << size << " bytes." << std::endl;
Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing.This might not work - the compiler doesn't have to maintain the order of variables on the stack, so
last
could end up before or afterfirst
and the other variables.