Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > WaitForSingleObeject runtime error

Reply
Thread Tools

WaitForSingleObeject runtime error

 
 
Larry
Guest
Posts: n/a
 
      01-27-2010
Hi,

I am getting this close to finish my tiny streaming server...having said
that I have a problem with the following code. It basically fires a runtime
error when I disconect from the sever! (closing the telnet window)

I wound up finding out that the error may be fired because of this line:

WaitForSingleObject(eventi[threadid], INFINITE);

If I replaceit with: Sleep(1000) everything goes ok....


/*
*
* Streaming Server v1.0 by THEARTOFWEB Software
*
*/

#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include <process.h>
#include <cstdlib>
#include <ctime>
#include "socket.h"
#include <boost/circular_buffer.hpp>
using namespace std;
using namespace boost;

const string CRLF = "\r\n";
const int numbuff = 3;

unsigned int __stdcall Consumer(void* sock);
unsigned int __stdcall Producer(void*);

void getDateTime(char * szTime);

enum buffer_status
{
BUFF_DONE = 1,
BUFF_EMPTY = 0
};

struct buffer
{
unsigned char data[1024];
int bytesRecorded;
int flag;
buffer(const unsigned char * data_, const int bytesRecorded_, const int
flag_) :
bytesRecorded(bytesRecorded_), flag(flag_)
{
copy(data_, data_ + bytesRecorded_, data);
}
};

struct circular
{
circular_buffer<buffer> cb;
};

map<int, circular> users;
map<int, circular>::iterator uit;
map<int, HANDLE> eventi;

int main()
{
// Launch Producer
unsigned int prodRet;
_beginthreadex(0,0,Producer,NULL,0,&prodRet);
if(prodRet)
cout << "Launched Producer Thread!" << endl;

// Set up server (port: 8000, maxconn: 10)
SocketServer sockIn(8000, 10);

while(1)
{
// ...wait for incoming connections...
Socket* s = sockIn.Accept();
unsigned int sockRet;
_beginthreadex(0,0,Consumer,s,0,&sockRet);
if(sockRet)
cout << "Spawned a new thread!" << endl;
}

sockIn.Close();

return EXIT_SUCCESS;
}

// Consumer
unsigned int __stdcall Consumer(void* sock)
{
Socket* s = (Socket*) sock;

s->SendBytes("Hello World!" + CRLF);

int threadid = (int)GetCurrentThreadId();

// Create Event & push it in the event map
HANDLE hevent = CreateEvent(NULL,FALSE,FALSE,NULL);
eventi.insert(make_pair(threadid,hevent));

// Prepare & add circular buffer to the map
circular c;
c.cb.set_capacity(numbuff);

for(int i = 0; i<numbuff; i++)
{
c.cb.push_back(buffer(NULL,0,BUFF_EMPTY));
}

users.insert(make_pair(threadid, c));

//
// TODO:
// Read data from the buffer
// and send it to the client
//
// When using push_back the oldest
// element in the circular buffer
// will be in the index 0
//

Sleep(500);

while(1)
{
// CALLBACK EVENT
WaitForSingleObject(eventi[threadid], INFINITE);
if(users[threadid].cb.at(0).flag == BUFF_DONE)
{
string line = (char*)users[threadid].cb.at(0).data;
int ret = s->SendBytes(line + CRLF);
if(SOCKET_ERROR == ret)
break;
}
}

// Close & remove event from event map
CloseHandle(eventi[threadid]);
eventi.erase(threadid);

// Remove buffer from the map
users.erase(threadid);

// Say bye to the client
s->SendBytes("Bye bye!" + CRLF);

// Disconnect client
cout << "Closing thread..." << endl;
s->Close();
delete s;
return 0;
}

// Producer
unsigned int __stdcall Producer(void*)
{
while(1)
{
Sleep(1000);
char szTime[30]; getDateTime(szTime);
for(uit=users.begin(); uit!=users.end(); ++uit)
{
users[uit->first].cb.push_back(buffer((unsigned char*)szTime,
30, BUFF_DONE));
SetEvent(eventi[uit->first]);
cout << "Producer is writing to: " << uit->first << endl;
}
}
return 0;
}

void getDateTime(char * szTime)
{
time_t rawtime = time(NULL);
struct tm timeinfo;
gmtime_s(&timeinfo, &rawtime);
strftime(szTime, 30, "%a, %d %b %Y %X GMT", &timeinfo);
}

// thanks

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-27-2010
Larry wrote:
> Hi,
>
> I am getting this close to finish my tiny streaming server...having
> said that I have a problem with the following code. It basically fires a
> runtime error


...with which we really can't help you - that's OS-specific..

> when I disconect from the sever! (closing the telnet window)
>
> I wound up finding out that the error may be fired because of this line:


"May be"? So, it's possible that it's not the call to
WaitForSingleObject but something else that screws up the memory
occupied by the actual 'eventi' map...

>
> WaitForSingleObject(eventi[threadid], INFINITE);
>
> If I replaceit with: Sleep(1000) everything goes ok....


Does the 'eventi' map actually contain a valid value with the key
'threadid' for passing it to 'WaitForSingleObject'? Those things need
to be checked, you know.

If access to 'data' in your 'buffer' is somehow incorrect (you need to
check), and you write beyond the boundary of 1024 elements, it's very
likely that you stomp all over the dynamically allocated memory used to
keep your 'eventi' map elements. Then attempt to access them can cause
undefined behaviour, which in your case ends up being a run-time error
reported by the OS.

> [..]
>
> struct buffer
> {
> unsigned char data[1024];
> int bytesRecorded;
> int flag;
> buffer(const unsigned char * data_, const int bytesRecorded_, const
> int flag_) :
> bytesRecorded(bytesRecorded_), flag(flag_)
> {
> copy(data_, data_ + bytesRecorded_, data);
> }
> };
>
> struct circular
> {
> circular_buffer<buffer> cb;
> };
>
> map<int, circular> users;
> map<int, circular>::iterator uit;
> map<int, HANDLE> eventi;
>
> int main()
> {
> [..]
> // thanks


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help: Runtime Error - Server Error in '/' Application Kevin Farless ASP .Net 13 10-03-2007 09:48 AM
Runtime.exec(String[]) Doesn't Always Work, bBut Runtime.exec(String) Does Hal Vaughan Java 11 05-22-2006 04:49 PM
Server Error in '/' Application. Runtime Error. Krunom Ancini ASP .Net 2 03-01-2005 10:07 AM
where can i find the lines of error in Runtime Error Page? Yongsub Eric Shin ASP .Net 7 01-14-2004 12:29 AM
fatal error CS0007: Unexpected common language runtime initialization error -- Polo Lee ASP .Net 0 07-07-2003 01:24 AM



Advertisments