Hi everyone,
I have one strange issue with the NDK 2.24. When calling DaemonFree the Application crashes. The reason (as far as I can tell) is:
void DaemonFree( HANDLE hEntry ) { DREC *pdr = (DREC *)hEntry; CHILD *pc; /* At this point we must have a semaphore */ if( !hDSem ) return; /* Enter our own lock */ SemPend( hDSem, SEM_FOREVER ); /* Sanity check */ if( pdr->Type!=SOCK_STREAM && pdr->Type!=SOCK_STREAMNC && pdr->Type!=SOCK_DGRAM ) goto errorout; /* Clear the record */ pdr->Type = 0; RequestedRecords--; /* Close the socket session of all children. This will */ /* cause them to eventually fall out of their code and */ /* close their sockets */ pc = pdr->pC; while( pc ) { if( pc->hTask ) { fdCloseSession( pc->hTask ); pc->hTask = 0; } pc = pc->pNext; } /* Close the socket (this will wake anyone who's using it) */ if( pdr->s != INVALID_SOCKET ) { fdClose( pdr->s ); pdr->s = INVALID_SOCKET; } /* If there are no more records, close the daemon task's */ /* file descriptor session. That will cause it to error */ /* out and remove itself */ if( !RequestedRecords ) { fdCloseSession( hDTask ); hDTask = 0; } errorout: /* Exit our lock */ SemPost( hDSem ); }
As you can see DaemonFree calls fdClose(pdr->s) and invalidates the socket (pdr->s = INVALID_SOCKET). However, after that the main daemon thread crashes in the following code:
if( bind( drec[i].s,(struct sockaddr *)&sin1, sizeof(sin1) ) < 0 ) { fdClose( drec[i].s ); drec[i].s = INVALID_SOCKET; }
Here fdClose tries to close the already closed socket again... which crashes since fdClose doesn't check for INVALID_SOCKET.
Any ideas why and how?
Greetings,
Thomas