2

I'm working on a multiclient chat project.

Here is my code:

struct RecvDataModel
{
    int sockAddr;
    char *inData;
};

void *ProcessData(void *arg);

void Client::Recv(int sockAddr, char *inData)
{
    RecvDataModel outData;
    outData.sockAddr = sockAddr;
    outData.inData = inData;
    pthread_t rThr;
    pthread_create(&rThr, NULL, ProcessData, (void*)&outData);
}

void *ProcessData(void *arg)
{
    RecvDataModel *inData = (RecvDataModel*)arg;
    cout << inData->inData << endl;
    return 0;
}

Basically if sockAddr (in Client::Recv) equals "55" ProcessData's cout function writing "31784736", if equals "0" cout's "5120"

That's my big problem! I can't continue without this! (I'm using eclipse C++) What's the problem? I have already looked some example projects like this: Link >>>

Community
  • 1
  • 1
freaka61
  • 35
  • 1
  • 7

2 Answers2

3

You're passing a pointer to a RecvDataModel which is a function-local variable. It will go out of scope at the end of the Client::Recv function.

Try allocating it with new instead:

RecvDataModel * outData = new RecvDataModel();
outData->sockAddr = sockAddr;
outData->inData = inData;
pthread_t rThr;
pthread_create(&rThr, NULL, ProcessData, outData);
James M
  • 18,506
  • 3
  • 48
  • 56
  • now it cout's "32041692" for sockAddr "0" Edit: sorry about this, it works! (not worked in the first because i have used "pthread_create(&rThr, NULL, ProcessData, &outData);" instead of "pthread_create(&rThr, NULL, ProcessData, outData);" thank you very much! :) – freaka61 Jan 27 '13 at 17:23
  • @freaka61 You may want to delete `inData` when you're done with it in `ProcessData()` to avoid leaking memory. – Joachim Isaksson Jan 27 '13 at 17:33
  • @JoachimIsaksson how? like close()? – freaka61 Jan 27 '13 at 17:36
  • hmm, can i delete it in ProcessData()? – freaka61 Jan 27 '13 at 17:38
  • like that? void Client::Recv(int sockAddr, char *inData) { ... delete outData; } and void *ProcessData(void *arg) { ... delete inData; } – freaka61 Jan 27 '13 at 17:40
  • @freaka61 Yes, you've allocated it with `new` and passed it to `ProcessData`. Once you're done with it in `ProcessData`, you'll need to do `delete inData;` or it will never be released. – Joachim Isaksson Jan 27 '13 at 17:40
  • @freaka61 Just noticed from your example above that you're asking if you should delete it twice, no you should not delete outData in Recv, you'll still want it to be allocated when you get to ProcessData. – Joachim Isaksson Jan 27 '13 at 17:52
  • @JoachimIsaksson if i delete in only ProcessData() will it be deleted(no memory usage)? – freaka61 Jan 27 '13 at 17:59
  • @JoachimIsaksson Err... there is an other problem. sockAddr works fine with struct but i can't take *inData? – freaka61 Jan 27 '13 at 18:14
2

Don't pass around pointers to local variables that go out of scope. As soon as you create that thread, outData isn't valid anymore, and so the pointer you gave to it is no good. You need to declare outData with a static qualifier, or allocate space for it dynamically so that it doesn't vanish when Client::Recv returns.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469