![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| performing cleanup when a job finishes | ChicagoBlues | Shell Programming and Scripting | 4 | 03-06-2008 12:41 PM |
| awk/sed/ksh script to cleanup /etc/group file | pdtak | Shell Programming and Scripting | 6 | 02-28-2008 03:33 AM |
| Help with cleanup | whdr02 | Shell Programming and Scripting | 2 | 01-25-2008 03:44 PM |
| Login ID cleanup | MILLERJ62 | AIX | 1 | 05-12-2006 05:20 AM |
| sendmail cleanup | thomi39 | UNIX for Dummies Questions & Answers | 1 | 02-23-2006 09:48 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
pthread_cleanup_push/pop - cleanup handler problem
Code:
void cleanUp_delete(void * p)
{
delete p;
p = NULL;
cout << "cleanUp_delete" << endl;
}
void connectionReader(void *thread,void*arg)
{
connection* c = (connection*) arg;
pthread_cleanup_push(cleanUp_delete,(void*)c);
int bytes;
char *buffer = new (char [512]);
pthread_cleanup_push(cleanUp_delete,(void*)buffer);
cout << "listening for " << c->socket << " " << inet_ntoa(c->addr) << endl;
while(true) {
if ((bytes = read(c->socket,(void*)buffer,sizeof(char[512]))) == -1){
cout << "receive() error" << endl;
break;
}
if(bytes == 0) break;
c->buffer.append(buffer);
cout << "received " << bytes << " bytes from " << inet_ntoa(c->addr) << endl;
cout << buffer << endl;
bzero(buffer,sizeof(char[512]));
}
cout << "closing connection to " << c->socket << " " << inet_ntoa(c->addr) << endl;
pthread_cleanup_pop(1);
pthread_cleanup_pop(1);
//c->~connection();
cout << "closed" << endl;
pthread_exit(NULL);
return;
}
consider this portion of code. if it wont cout that cleanUp_delete, but if uncomment thqat c->~connection(), it will segfault. so it seems the cleanUp_delete was called, because c is deleted, but why would my message not be printed? the destructor of c, called from the cleanUp handler should cout also, but doesnt. im not sure if those handlers are called in a strange way or not at all. help me out please. sonicx |
|
||||
|
You need either two different handlers to deal with the different types, or one handler that deals with different types.. Code:
struct mydata
{
connection *c;
char *buffer;
};
static void mycleanup(void *arg)
{
struct mydata *data=(struct mydata *)arg;
delete data->c;
delete data->buffer;
}
....
{
struct mydata data;
data.c=(connection *)arg;
data.buffer=new char[...]
pthread_cleanup_push(mycleanup,&mydata);
.....
pthread_cleanup_pop(1);
...
}
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|