some code refraction to get rid of memeory leakage

This commit is contained in:
Dhanya Maliakal 2015-02-04 16:11:13 +01:00
parent f30833a460
commit e975a75be9
5 changed files with 33 additions and 27 deletions

View File

@ -103,7 +103,6 @@ enum communicationProtocol{
nsent(0), nsent(0),
total_sent(0)// sender (client): where to? ip total_sent(0)// sender (client): where to? ip
{ {
// strcpy(hostname,host_ip_or_name); // strcpy(hostname,host_ip_or_name);
struct hostent *hostInfo = gethostbyname(host_ip_or_name); struct hostent *hostInfo = gethostbyname(host_ip_or_name);
if (hostInfo == NULL){ if (hostInfo == NULL){

View File

@ -1328,8 +1328,8 @@ int UDPBaseImplementation::startListening(){ FILE_LOG(logDEBUG) << __AT__ << " s
thread_started = 1; thread_started = 1;
int i,total; int total;
int lastpacketoffset, expected, rc, rc1,packetcount, maxBufferSize, carryonBufferSize; int lastpacketoffset, expected, rc,packetcount, maxBufferSize, carryonBufferSize;
uint32_t lastframeheader;// for moench to check for all the packets in last frame uint32_t lastframeheader;// for moench to check for all the packets in last frame
char* tempchar = NULL; char* tempchar = NULL;
int imageheader = 0; int imageheader = 0;
@ -1534,9 +1534,8 @@ int UDPBaseImplementation::startWriting(){ FILE_LOG(logDEBUG) << __AT__ << " sta
char* wbuf[numListeningThreads];//interleaved char* wbuf[numListeningThreads];//interleaved
char *d=new char[bufferSize*numListeningThreads]; char *d=new char[bufferSize*numListeningThreads];
int xmax=0,ymax=0; int xmax=0,ymax=0;
int ret,i,j; int ret,i;
int packetsPerThread = packetsPerFrame/numListeningThreads; int packetsPerThread = packetsPerFrame/numListeningThreads;
int loop;
while(1){ while(1){

View File

@ -67,7 +67,6 @@ UDPStandardImplementation::UDPStandardImplementation()
rawDataReadyCallBack = NULL; rawDataReadyCallBack = NULL;
pRawDataReady = NULL; pRawDataReady = NULL;
initializeMembers();
//mutex //mutex
pthread_mutex_init(&dataReadyMutex,NULL); pthread_mutex_init(&dataReadyMutex,NULL);
@ -75,6 +74,8 @@ UDPStandardImplementation::UDPStandardImplementation()
pthread_mutex_init(&progress_mutex,NULL); pthread_mutex_init(&progress_mutex,NULL);
pthread_mutex_init(&write_mutex,NULL); pthread_mutex_init(&write_mutex,NULL);
initializeMembers();
//to increase socket receiver buffer size and max length of input queue by changing kernel settings //to increase socket receiver buffer size and max length of input queue by changing kernel settings
if(system("echo $((100*1024*1024)) > /proc/sys/net/core/rmem_max")) if(system("echo $((100*1024*1024)) > /proc/sys/net/core/rmem_max"))
cout << "\nWARNING: Could not change socket receiver buffer size in file /proc/sys/net/core/rmem_max" << endl; cout << "\nWARNING: Could not change socket receiver buffer size in file /proc/sys/net/core/rmem_max" << endl;
@ -1567,8 +1568,8 @@ int UDPStandardImplementation::startListening(){
thread_started = 1; thread_started = 1;
int i,total; int total;
int lastpacketoffset, expected, rc, rc1,packetcount, maxBufferSize, carryonBufferSize; int lastpacketoffset, expected, rc,packetcount, maxBufferSize, carryonBufferSize;
uint32_t lastframeheader;// for moench to check for all the packets in last frame uint32_t lastframeheader;// for moench to check for all the packets in last frame
char* tempchar = NULL; char* tempchar = NULL;
int imageheader = 0; int imageheader = 0;
@ -1750,6 +1751,8 @@ int UDPStandardImplementation::startListening(){
if(tempchar) {delete [] tempchar;tempchar = NULL;} if(tempchar) {delete [] tempchar;tempchar = NULL;}
pthread_exit(NULL); pthread_exit(NULL);
} }
if(tempchar) {delete [] tempchar;tempchar = NULL;}
} }
return OK; return OK;
@ -1782,9 +1785,8 @@ int UDPStandardImplementation::startWriting(){
char* wbuf[numListeningThreads];//interleaved char* wbuf[numListeningThreads];//interleaved
char *d=new char[bufferSize*numListeningThreads]; char *d=new char[bufferSize*numListeningThreads];
int xmax=0,ymax=0; int xmax=0,ymax=0;
int ret,i,j; int ret,i;
int packetsPerThread = packetsPerFrame/numListeningThreads; int packetsPerThread = packetsPerFrame/numListeningThreads;
int loop;
while(1){ while(1){
@ -1880,7 +1882,7 @@ int loop;
for(i=0;i<numListeningThreads;++i){ for(i=0;i<numListeningThreads;++i){
while(!fifoFree[i]->push(wbuf[i])); while(!fifoFree[i]->push(wbuf[i]));
#ifdef VERYDEBUG #ifdef VERYDEBUG
cout << ithread << ":" << i+j << " fifo freed:" << (void*)wbuf[i] << endl; cout << ithread << ":" << i << " fifo freed:" << (void*)wbuf[i] << endl;
#endif #endif
} }
@ -1888,10 +1890,12 @@ int loop;
} }
else{ else{
//copy to gui //copy to gui
if((packetsPerFrame * numpackets) == bufferSize){
copyFrameToGui(NULL,-1,wbuf[0]+HEADER_SIZE_NUM_TOT_PACKETS); copyFrameToGui(NULL,-1,wbuf[0]+HEADER_SIZE_NUM_TOT_PACKETS);
#ifdef VERYVERBOSE #ifdef VERYVERBOSE
cout << ithread << " finished copying" << endl; cout << ithread << " finished copying" << endl;
#endif #endif
}//else cout << "unfinished buffersize" << endl;
while(!fifoFree[0]->push(wbuf[0])); while(!fifoFree[0]->push(wbuf[0]));
#ifdef VERYVERBOSE #ifdef VERYVERBOSE
cout<<"buf freed:"<<(void*)wbuf[0]<<endl; cout<<"buf freed:"<<(void*)wbuf[0]<<endl;
@ -1959,6 +1963,7 @@ int loop;
#endif #endif
} }
delete [] d;
return OK; return OK;
} }
@ -2023,9 +2028,10 @@ int i;
//push the last buffer into fifo //push the last buffer into fifo
if((myDetectorType == EIGER) && (rc < 266240) )//for eiger throw away incomplete frames if((myDetectorType == EIGER) && (rc < 266240) )//for eiger throw away incomplete frames
fifoFree[ithread]->push(buffer[ithread]); fifoFree[ithread]->push(buffer[ithread]);
else if(rc > 0){cout<< ithread << " last rc:"<<rc<<endl; else if(rc > 0){
pc = (rc/onePacketSize); pc = (rc/onePacketSize);
#ifdef VERYDEBUG #ifdef VERYDEBUG
cout << ithread << " last rc:"<<rc<<endl;
cout << ithread << " *** last packetcount:" << pc << endl; cout << ithread << " *** last packetcount:" << pc << endl;
#endif #endif
(*((uint16_t*)(buffer[ithread]))) = pc; (*((uint16_t*)(buffer[ithread]))) = pc;

View File

@ -85,6 +85,7 @@ int main(int argc, char *argv[]) {
user->stop(); user->stop();
} }
delete user;
cout << "Goodbye!" << endl; cout << "Goodbye!" << endl;
return 0; return 0;
} }

View File

@ -26,7 +26,8 @@ using namespace std;
slsReceiverTCPIPInterface::~slsReceiverTCPIPInterface() { slsReceiverTCPIPInterface::~slsReceiverTCPIPInterface() {
if(socket) delete socket; if(socket) {delete socket; socket=NULL;}
if(receiverBase) {delete receiverBase; receiverBase=NULL;}
closeFile(0); closeFile(0);
} }
@ -1221,8 +1222,8 @@ int slsReceiverTCPIPInterface::gotthard_read_frame(){
} }
//all adc //all adc
else{ else{
//ignore if half frame is missing /*//ignore if half frame is missing
if ((bindex != 0xFFFFFFFF) && (bindex2 != 0xFFFFFFFF)){ if ((bindex != 0xFFFFFFFF) && (bindex2 != 0xFFFFFFFF)){*/
//should be same frame //should be same frame
if (index == index2){ if (index == index2){
@ -1239,11 +1240,11 @@ int slsReceiverTCPIPInterface::gotthard_read_frame(){
} }
}else }else
cout << "different frames caught. frame1:"<< hex << index << ":"<<pindex<<" frame2:" << hex << index2 << ":"<<pindex2<<endl; cout << "different frames caught. frame1:"<< hex << index << ":"<<pindex<<" frame2:" << hex << index2 << ":"<<pindex2<<endl;
} /*}
else{ else{
index = startIndex - 1; index = startIndex - 1;
cout << "Missing Packet,Not sending to gui" << endl; cout << "Missing Packet,Not sending to gui" << endl;
} }*/
} }
arg = (index - startIndex); arg = (index - startIndex);