Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 7 Apr 2001 01:17:46 -0400
From:      "Brian M. King" <raider1@rochester.rr.com>
To:        <freebsd-questions@FreeBSD.ORG>
Subject:   C++
Message-ID:  <000b01c0bf22$14f70460$0d6e1842@King.rochester.rr.com>

next in thread | raw e-mail | index | archive | help
This is a multi-part message in MIME format.

------=_NextPart_000_0007_01C0BF00.8D92FEA0
Content-Type: multipart/alternative;
	boundary="----=_NextPart_001_0008_01C0BF00.8D92FEA0"


------=_NextPart_001_0008_01C0BF00.8D92FEA0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

    Hello. I am currently in the process of developing an IRCd in C++ on =
FreeBSD and while I was creating some test code I discovered a problem =
and no programmers that I know seem to have any ideas as to why its =
happening. I have the main() opening a socket for listening and =
accepting connections. I also have a threaded function (Using POSIX =
threads) that reads data from all the sockets and stores it into an STL =
Queue system. I then have several work threads running the same function =
that reads from the STL Queue list to handle the data appropriatly. In =
the first thread that reads from the sockes I am allocating the memory =
for each container element using 'char *msg =3D new char[size]' .. The =
problem I am having is in the worker threads that handle the queues I do =
a 'char *msg; msg =3D MessageQueue.front(); MessageQueue.pop(); if (msg =
!=3D NULL) { delete [] msg; }' When it tries to delete the allocated =
memory I get the error 'Junk pointer, too high to make sense.' but I've =
used this method before without a problem. I am running FreeBSD =
4.2-RELEASE and am compiling with g++ .. I am attaching the test program =
I've written to develop the STL Container system I will need in the ircd =
itself.

To compile: g++ -pthread -Wall -g -o sin sin2.cpp
To Run: ./sin

Open a socket connection to the box this program is running on on port =
1600 and send data to it, you will see the error (Which occurs in the =
queue_handler function).

-Brian

------=_NextPart_001_0008_01C0BF00.8D92FEA0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.2614.3500" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; Hello. I am =
currently in the=20
process of developing an IRCd in C++ on FreeBSD and while I was creating =
some=20
test code I discovered a problem and no programmers that I know seem to =
have any=20
ideas as to why its happening. I have the main() opening a socket for =
listening=20
and accepting connections. I also have a threaded function (Using POSIX =
threads)=20
that reads data from all the sockets and stores it into an STL Queue =
system. I=20
then have several work threads running the same function that reads from =
the STL=20
Queue list to handle the data appropriatly. In the first thread that =
reads from=20
the sockes I am allocating the memory for each container element using =
'char=20
*msg =3D new char[size]' .. The problem I am having is in the worker =
threads that=20
handle the queues I do a 'char *msg; msg =3D MessageQueue.front();=20
MessageQueue.pop(); if (msg !=3D NULL) { delete [] msg; }' When it tries =
to delete=20
the allocated memory I get the error 'Junk pointer, too high to make =
sense.' but=20
I've used this method before without a problem. I am running FreeBSD =
4.2-RELEASE=20
and am compiling with g++ .. I am attaching the test program I've =
written to=20
develop the STL Container system I will need in the ircd =
itself.</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>To compile: g++ -pthread -Wall -g -o =
sin=20
sin2.cpp</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>To Run: ./sin</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Open a socket connection to the box =
this program is=20
running on on port 1600 and send data to it, you will see the error =
(Which=20
occurs in the queue_handler function).</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>-Brian</FONT></DIV></BODY></HTML>

------=_NextPart_001_0008_01C0BF00.8D92FEA0--

------=_NextPart_000_0007_01C0BF00.8D92FEA0
Content-Type: application/octet-stream;
	name="sin2.cpp"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="sin2.cpp"

#include <sys/types.h>=0A=
#include <sys/socket.h>=0A=
#include <netinet/in.h>=0A=
#include <arpa/inet.h>=0A=
#include <netdb.h>=0A=
#include <stdio.h>=0A=
#include <unistd.h>=0A=
#include <pthread.h>=0A=
#include <map>=0A=
#include <queue>=0A=
#include <fcntl.h>=0A=
=0A=
void *queue_handler(void *);=0A=
void *read_data(void *);=0A=
=0A=
#define SUCCESS 0=0A=
#define ERROR   1=0A=
=0A=
#define END_LINE 0x0A=0A=
#define SERVER_PORT 1600=0A=
#define MAX_MSG 1=0A=
=0A=
queue<char *> MessageQueue;=0A=
map<int,int> SockMap;=0A=
map<int,int>::iterator iter;=0A=
=0A=
int sd, i =3D 0, x =3D 0;=0A=
socklen_t cliLen;=0A=
pthread_t mt;=0A=
pthread_t qh1, qh2, qh3, qh4, qh5;=0A=
pthread_mutex_t mutex;=0A=
char *line;=0A=
struct sockaddr_in cliAddr, servAddr;=0A=
=0A=
int main () {=0A=
   pthread_mutex_init(&mutex, pthread_mutexattr_default);=0A=
   sd =3D socket(AF_INET, SOCK_STREAM, 0);=0A=
   if (sd < 0) {=0A=
      perror("cannot open socket ");=0A=
      return ERROR;=0A=
   }=0A=
  =0A=
   servAddr.sin_family =3D AF_INET;=0A=
   servAddr.sin_addr.s_addr =3D htonl(INADDR_ANY);=0A=
   servAddr.sin_port =3D htons(SERVER_PORT);=0A=
  =0A=
   if (bind(sd, (struct sockaddr *) &servAddr, sizeof(servAddr))<0) {=0A=
      perror("cannot bind port ");=0A=
      return ERROR;=0A=
   }=0A=
=0A=
   listen(sd,5);=0A=
   printf("Waiting for data on port TCP %u\n",SERVER_PORT);=0A=
=0A=
   pthread_create(&mt, NULL, read_data, NULL);=0A=
   pthread_create(&qh1, NULL, queue_handler, NULL);=0A=
   pthread_create(&qh2, NULL, queue_handler, NULL);=0A=
   pthread_create(&qh3, NULL, queue_handler, NULL);=0A=
   pthread_create(&qh4, NULL, queue_handler, NULL);=0A=
   pthread_create(&qh5, NULL, queue_handler, NULL);=0A=
=0A=
   while (1) {=0A=
      cliLen =3D sizeof(cliAddr);=0A=
      if ((SockMap[i] =3D accept(sd, (struct sockaddr *) &cliAddr, =
&cliLen)) !=3D -1) {=0A=
         fcntl(SockMap[i], F_SETFL, O_NONBLOCK);=0A=
         printf("Got a connection\n");=0A=
         i++;=0A=
      }=0A=
   }=0A=
   return 1;=0A=
}=0A=
=0A=
void *read_data(void *)=0A=
{=0A=
   char buf[1025];=0A=
=0A=
   map<int,char *> flack;=0A=
   while (1) {=0A=
      for (iter =3D SockMap.begin(); iter !=3D SockMap.end(); iter++) {=0A=
         if (iter->second !=3D 0) {=0A=
            memset(buf, 0, 1025);=0A=
            int size =3D recv(iter->second, buf, 1024, MSG_DONTWAIT);=0A=
=0A=
            if (size) {=0A=
               char *s, *e;=0A=
               s =3D buf;=0A=
               while ((e =3D strchr(s, '\n')) !=3D NULL) {=0A=
                  *e =3D 0;=0A=
                  if (flack.find(iter->second) !=3D flack.end() && =
flack[iter->second] !=3D NULL) {=0A=
                     flack[iter->second] =3D (char *) =
realloc(flack[iter->second], strlen(flack[iter->second]) + strlen(s) + =
1);=0A=
                     strcat(flack[iter->second], s);=0A=
                     char *msg =3D (char =
*)malloc(strlen(flack[iter->second]) + 1);  =0A=
                     strcpy(msg, flack[iter->second]);=0A=
                     pthread_mutex_lock(&mutex);   =0A=
                     MessageQueue.push(flack[iter->second]);=0A=
                     pthread_mutex_unlock(&mutex);=0A=
                     free(flack[iter->second]);=0A=
                     flack.erase(iter->second);=0A=
                  } else {=0A=
                     char *msg =3D (char *)malloc(strlen(s) + 1);  =0A=
                     strcpy(msg, s);=0A=
                     pthread_mutex_lock(&mutex);=0A=
                     MessageQueue.push(s);=0A=
                     pthread_mutex_unlock(&mutex);=0A=
                  }=0A=
                  s =3D ++e;=0A=
               }=0A=
               if (*s !=3D 0) {=0A=
                  if (flack.find(iter->second) !=3D flack.end()) {=0A=
                     flack[iter->second] =3D (char *) =
realloc(flack[iter->second], strlen(flack[iter->second]) + strlen(s) + =
1);=0A=
                     strcat(flack[iter->second], s);=0A=
                  } else {=0A=
                     flack[iter->second] =3D (char *) malloc(strlen(s) + =
1);=0A=
                     strcpy(flack[iter->second], s);=0A=
                  }=0A=
               }=0A=
            }=0A=
         }=0A=
      }=0A=
      sleep(1);=0A=
   }=0A=
   return NULL;=0A=
}=0A=
void *queue_handler(void *)=0A=
{=0A=
   while (1) {    =0A=
      char *msg =3D NULL;=0A=
      pthread_mutex_lock(&mutex);=0A=
      if (MessageQueue.size()) { =0A=
         msg =3D MessageQueue.front();   =0A=
         MessageQueue.pop(); =0A=
      }=0A=
      pthread_mutex_unlock(&mutex);=0A=
      if (msg !=3D NULL) {=0A=
         //do_processing();   =0A=
         free(msg);  =0A=
      }=0A=
   }=0A=
}=0A=

------=_NextPart_000_0007_01C0BF00.8D92FEA0--


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?000b01c0bf22$14f70460$0d6e1842>