I am doing a application witch uses sockets so I am holding in an array the sockets handles.I have the following code:
while(0 == 0){
int * tx = (int*)(malloc((nr_con + 2) * sizeof(int)));
if (conexiuni != NULL)
{
syslog(LOG_NOTICE,"Ajung la eliberare %d",nr_con);
memcpy(&tx[0],&conexiuni[0],(sizeof(int) * (nr_con)));
syslog(LOG_NOTICE,"Ajung la eliberare %d",nr_con);
free(conexiuni);
}
conexiuni = tx;
syslog(LOG_NOTICE,"Ajung la mama %d",nr_con);
//The line bellow causes a segfault at second connection
if ((conexiuni[nr_con] = accept(hsock,(sockaddr*)(&sadr),&addr_size)) != -1)
{
nr_con++;
syslog(LOG_NOTICE,"Primesc de la %s",inet_ntoa(sadr.sin_addr));
syslog(LOG_NOTICE,"kkt %d",conexiuni[nr_con - 1]);
int * sz = (int*)malloc(sizeof(int));
*sz = conexiuni[nr_con - 1];
syslog(LOG_NOTICE,"after %d",*sz);
pthread_create(&tidi,0,&ConexiuniHandler, sz);
}
}
When I connect the second time when I assign the array the program crashes. What am I doing wrong? I tried the same code on Windows and it works well but on Linux it crashes.
I assume that what you are wanting to do is to have a server that is accepting connections and then as the connections are accepted, you start a thread to handle that connection request. So each time you do an accept you are wanting to start up a thread and give it the socket handle. You are also keeping up with all of the socket handles in an array which is dynamically increased as you accept new connection requests.
Following is a suggested method. I have not done any testing nor have I even compiled this code segment however it is a place to start. One thing that I am doing is increasing the array of socket handles by blocks of 16 each time I do a resize of the array. I am doing this because it can make the job of the memory manager a bit easier and reduce the amount of fragmentation by reducing the number of calls to
malloc().However there are a few issues with something like this. First of all in the example above I am not handling an out of memory error should
malloc()return a NULL pointer due to being unable to provide the memory request.The second issue is the possibility that the thread will not access the pointer to the socket before the array is dynamically extended rendering the pointer provided invalid since it was freed during the dynamic reallocation. So if you have lots of connections coming in quickly, this may be a problem. At a minimum the first thing the thread should do is make a local copy of the socket handle.
Another question is how are you going to go back over the array to determine which sockets are still valid and open and which are stale with the connection closed. Do you just keep dynamically allocating space as connection requests come in until you run out of memory after a few days of your server being up and running?
Rather than using int, you really should be using SOCKET since that it the actual data type. I realize that in most cases, a SOCKET is actually an int, however it is usually better to be precise in these matters.