Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
i don't know why parameters passed to pthread changes automatically.
i'll show you my code
the debug result is this :
begin of while while() =begin= 0 ===
params: a84a5310
->socket: a84a4e70
->connection: a84a52d0(4)
->webserver: a84a4ea0(0)
===== begin of while while() =end====
===== end of while() =begin= 1709692672 ===
params: a84a5310
->socket: a84a4e70
->connection: a84a52d0(4)
->webserver: a84a4ea0(0)
===== end of while() =end====
Accepting...
===== in thread() =begin= 1709692672 ===
params: aacf4240//wrong expected a84a5310
->socket: a84a5310 //wrong expected a84a4e70
->connection: a84a4ea0(0)//wrong expected a84a52d0(4)
->webserver: 65e7d700(0) //wrong expected a84a4ea0(0)
===== in thread() =end====
this is struct that i pass to the thread :
typedef struct InfoServer{
ServerTCP* server;
WebServer* webserver;
ConnServer* serverConn;
}ServerInfo;
this is debug function :
void* show_params(ServerInfo* params, char* where, int n) {
printf("===== %s =begin= %d ===\n",where, n);
printf("params: %x\n",
params);
printf("->socket: %x\n",
params->server);
printf("->connection: %x(%d)\n",
params->serverConn, params->serverConn->getConn());
printf("->webserver: %x(%d)\n",
params->webserver);
printf("===== %s =end====\n",where);
fflush(stdout);
}
this is main function :
void *manageConnection(void *serverConn);
void fineRichiesta(ServerInfo * serverinformation);
int main (int argc , char* argv[]){
if(argc!=2){
printf("USAGE: %S , PORT \n",argv[0]);
fflush(stdout);
exit(1);
}
int port = atoi(argv[1]);
ServerTCP* tserver= new ServerTCP(port);
WebServer* webserv= new WebServer();
bool exit = false;
while( true ){
pthread_t manageConnection_thread;
ConnServer* serverC= tserver->acceptConn();
ServerInfo* serverinfo = (ServerInfo*) malloc(sizeof(ServerInfo));
serverinfo->server = tserver;
serverinfo->webserver = webserv;
serverinfo->serverConn =serverC;
show_params(serverinfo,"starting in while()",0);
if(pthread_create(&manageConnection_thread,
NULL,
manageConnection,
(void*) &serverinfo)) {
error("Error Creating server");
}
show_params(serverinfo,"end of while()", manageConnection_thread);
}
delete( tserver);
delete( webserv);
return 0;
}
here is thread code :
void *manageConnection(void * serverinformation){
ServerInfo *serverinfo = (ServerInfo*)serverinformation;
show_params(serverinfo,"in thread()", pthread_self());
ConnServer * srvConn = serverinfo->serverConn;
char* answ;
if(srvConn){
answ = (char*)srvConn->riceviServer();
}
else {
printf("im in else\n");
fflush(stdout);
}
if(!answ){
printf("finerichiesta\n");
fflush(stdout);
fineRichiesta(serverinfo);
}
printf("answ: %s\n", answ);
fflush(stdout);
char* file = serverinfo->webserver->getFile(answ);
srvConn->inviaServer(file);
fineRichiesta( serverinfo);
free(answ);
return NULL;
}
function :
void fineRichiesta(ServerInfo * serverinformation){
printf("im in fine richiesta\n");
fflush(stdout);
serverinformation->server->disconnect(serverinformation->serverConn);
free(serverinformation);
pthread_exit(NULL);
}
Any help would be amazing , im getting crazy -
i dont know if my question is best formulated -
thanks in advance ...
Your pthread_create call is passing the wrong void * value to the thread start routine manageConnection.
It is passing the address of the serverInfo variable:
pthread_create(&manageConnection_thread,
NULL,
manageConnection,
(void*) &serverinfo)
It should be passing value of the serverInfo variable, which points to a filled in ServerInfo:
pthread_create(&manageConnection_thread,
NULL,
manageConnection,
(void*) serverinfo) // <-- note: ampersand removed here
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 days ago.
Improve this question
There are 2 message queues and 2 queue ids. When I call msgrcv, the queue id that is not associated with this msgrcv call gets altered. Its declared globally. When I make it static global the value does not change. But I need to know the root cause of this issue. The code snippet is below
int xmlrpcqid;
rtspbuf rpcbuf;
int statusqid;
msqbuf sbuf;
static void *getXmlResponse(void *arg);
printf ("*******msg qid position 1 = %d\n", statusqid);
while(!eventLoopWatchVariable)
{
printf ("*******msg qid position 2 = %d \n", statusqid);
if(msgrcv(xmlrpcqid, &rpcbuf, sizeof(rtspbuf), 0, MSG_NOERROR) < 0)
{
printf("msgrcv Fail \n");
}
else {
printf ("*******msg qid 3 = %d \n", statusqid);
}
}
// not adding remaining code
}
int main {
if ((xmlrpcqid = msgget (1256, IPC_CREAT | 0666 )) < 0)
printf ("login msgget failed\n");
if ((statusqid = msgget (1234, IPC_CREAT | 0666 )) < 0)
printf ("status msgget failed\n");
pthread_t xmlResponse_thr = -1;
if(pthread_create(&xmlResponse_thr, NULL, getXmlResponse, NULL))
{
printf("pthread_create failed for getXmlResponse\n");
return -1;
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am trying to figure out how to adapt this sample in order to redirect the output of a console window to a text box inside another process.
Unfortunately, it seems that the reader will never receive any input.
Further debugging shows that the call to SetHandleInformation always aborts with Error 6: Invalid Handle. The value of hPipeOutRd does not look bad, it is something like 0x00000244.
This reproduces the matter:
int main(int argc, char *argv[])
{
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
int result = 0;
HANDLE hPipeOutRd = INVALID_HANDLE_VALUE; // This end is passed to the pipe reader
HANDLE hPipeOutWr = INVALID_HANDLE_VALUE; // This end is passed to the child process
if ( result == 0 && !::CreatePipe( &hPipeOutRd, &hPipeOutWr, &sa, 4096 ) )
{
result = -1;
printf("Error: %u\r\n", GetLastError() );
}
if ( result == 0 && !::SetHandleInformation( &hPipeOutRd, HANDLE_FLAG_INHERIT, 0 ) ) // This fails with invalid handle
{
result = -1;
printf("Error: %u\r\n", GetLastError() );
}
return result;
}
Any ideas why?
A HANDLE is already a pointer. You don't take its address unless it is an out parameter.
Just take the & out of your SetHandleInformation call.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
This is my current MD5 function. When used on windows 8.1 it always returns a value, but when run on windows 7 it only returns a value about 50% of the time. That is pretty peculiar to me. Any ideas?
It turns out that it is dodgy on both win7 and win8. Apparently the call to CryptGetHashParam sometimes fails with ERROR_MORE_DATA.
std::string MD5(std::string input)
{
HCRYPTPROV CryptProv;
HCRYPTHASH CryptHash;
BYTE BytesHash[33];//!
DWORD dwHashLen;
std::string final;
if (CryptAcquireContext(&CryptProv,NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET))
{
if (CryptCreateHash(CryptProv, CALG_MD5, 0, 0, &CryptHash))
{
if (CryptHashData(CryptHash, (BYTE*)input.c_str(), input.length(), 0))
{
if (CryptGetHashParam(CryptHash, HP_HASHVAL, BytesHash, &dwHashLen, 0))
{
final.clear();
std::string hexcharset = "0123456789ABCDEF";
for (int j = 0; j < 16; j++)
{
final += hexcharset.substr(((BytesHash[j] >> 4) & 0xF), 1);
final += hexcharset.substr(((BytesHash[j]) & 0x0F), 1);
}
}
}
}
}
CryptDestroyHash(CryptHash);
CryptReleaseContext(CryptProv, 0);
return final;
}
int _tmain(int argc, _TCHAR* argv[])
{
char *pp = "derp";
std::string derp = MD5(std::string(pp));
printf("%s\n", derp.c_str());
system("pause");
return 0;
}
I have a fantastic idea: add else clauses to those conditions so that you know which one is failing. Put output in those clauses so that you can see detail on the error conditions. Then you will know what is wrong with your function.
I have an application and I am analyzing memory crash dumps of this software.
struct GPS_CONNECTION
{
int sockfd;
std::string sendbuf, recvbuf;
struct sockaddr_in remoteaddr;
};
vector <GPS_CONNECTION> GPSC;
--------------------------------
(cut)
--------------------------------
fd_set master, gps_master, read_fds, gps_read_fds, write_fds, gps_write_fds;
for (;;)
{
/* Clear */
FD_ZERO(&gps_read_fds);
FD_ZERO(&gps_write_fds);
/* read_fds */
gps_read_fds = gps_master;
/* write_fds */
for (int i=0; i < GPSC.size(); i++)
{
if (GPSC[i].sendbuf.empty())
{
continue;
}
FD_SET(GPSC[i].sockfd, &gps_write_fds);
}
/* Timeout struct */
tv.tv_sec = 0;
tv.tv_usec = 0;
/* selectuj write */
if (select(gps_fdmax+1, &gps_read_fds, &gps_write_fds, NULL, &tv) == -1)
{
perror("select");
return 7;
}
--------------------------------
(cut)
--------------------------------
}
GDB crash dump says software is crashed in line:
443 if (GPSC[i].sendbuf.empty())
When I was analyzing the variables I saw this:
(gdb) print i
$1 = -1214807923
I don't understand how this value was overwritten? I don't see any stack overflow issue here, can anyone explain the reason of this crash?
This problem appears recurring - once in 2-days, this is a server working 24/7/365.
After g++ expanding this code that results:
for (int i=0; i < GPSC.size(); i++)
{
if (GPSC[i].sendbuf.empty())
{
continue;
}
__asm__ __volatile__ ("btsl %1,%0" : "=m" (((&gps_write_fds)->fds_bits)[((GPSC[i].sockfd) / (8 * sizeof (__fd_mask)))]) : "r" (((int) (GPSC[i].sockfd)) % (8 * sizeof (__fd_mask))) : "cc","memory");
}
From the small code snippet you shared, it's hard to say what the problem really is.
I can only suspect that the result of GPSC.size() is bigger than what an int can store, therefore causing an overflow on i after some iterations.
Code will crashing because of multi-threading that i will not consider before, i think that is working in one thread only, my mistake.
I'll start with the code:
typedef std::vector<unsigned char> CharBuf;
static const int RCV_BUF_SIZE = 1024;
SOCKET m_socket = a connected and working socket;
// ...
CharBuf buf; // Declare buffer
buf.resize(RCV_BUF_SIZE); // resize buffer to 1024
char* p_buf = reinterpret_cast<char*>(&buf[0]); // change from unsigned char to char
//char p_buf[RCV_BUF_SIZE];
int ret = recv(m_socket, p_buf, RCV_BUF_SIZE, 0); // Does not work
for (int i=0; i<RCV_BUF_SIZE; ++i) // Works (does not crash, so the buffer is ok)
char c = p_buf[i];
//...
Now when I run this code ret becomes -1 and WSAGetLastError() returns 10014 which means the pointer is bad.
However I can't see why this shouldn't work? If I comment out the reinterpret_cast line and use the line below it works!
It could be argued that reinterpret_cast is risky, but I think it should be ok as both unsigned char and signed char has the exact same size.
std::vectors should be safe to address directly in memory as far as I know as well.
The funny part is that when I do the same thing with the same vector-type in send() it works! Send function:
void SendData(const CharBuf& buf)
{
buf.resize(RCV_BUF_SIZE); // resize buffer to 1024
const char* p_buf = reinterpret_cast<const char*>(&buf[0]); // change from unsigned char to char
int ret = send(m_socket, p_buf, (int)buf.size(), 0); // Works
}
As we see, no difference except CharBuf being const in this case, can that change anything?
Why is recv() more sensitive than send()? How can recv() even know the pointer is invalid (which it obviously isn't)?? all it should see is a char array!
As per request my whole receive function (bear in mind that I can't spell out every function in it, but I think they should be fairly self-explanatory.
bool TcpSocket::ReceiveData(CharBuf* pData)
{
if (!CheckInitialized("ReceiveData"))
return false;
if (m_status != CONNECTED_STAT)
{
AddToErrLog("Socket not connected", 1, "ReceiveData");
return false;
}
int ret;
pData->resize(RCV_BUF_SIZE);
char* p_buf = reinterpret_cast<char*>(&pData[0]);
ret = recv(m_socket, p_buf, RCV_BUF_SIZE, 0);
switch (ret)
{
case 0: // Gracefully closed
AddToLog("Connection gracefully closed", 2);
Shutdown(); // The connection is closed, no idea to keep running
return true;
case SOCKET_ERROR: // Error
ret = WSAGetLastError();
if (ret == 10004) // This indicates the socket was closed while we were waiting
AddToLog("Socket was shut down while waiting for data", 1, "ReceiveData(1)");
else
AddToErrLog("Receive data failed with code: " + CStr(ret));
AddToLog("Connection ended with error", 2);
Shutdown();
return false;
default: // Normal operation
pData->resize(ret); // Remove unused space
return true;
}
}
Never mind. I found it while I was pasting the function. Like always, you find your error when you try to explain it for someone else :)
I leave it up to the reader to figure out what was wrong, but I'll give &pData[0] as a hint.
Thanks for your help :D
Found the answer myself while pasting the whole function, &pData[0] is a hint.