Seg Fault Mem Leak

mmoose68's Avatar, Join Date: Jun 2009
Go4Expert Member
Alright, this function is in a thread which is reading lines from a file which will never be longer than 16 chars. inside the while(1) loop, if I comment out everything except an output of ptrr and the fgets, there is no seg fault. Also, I can comment out the push_backs and I still get the seg fault. Can someone please help me figure this one out?
Code:
void * read_file(void*){
	FILE * oar1 ;
	oar1 = fopen("/dev/ttyT8S0","r");
	char *ptrr = new char[16] ;
	char *index = new char[16] ;
	char *value = new char[16] ;
	long ind = 0 ;
	long vl = 0 ;
	while(1){
		fgets(ptrr, 16, oar1);
		if(atoi(ptrr)!=0){
			index = strtok(ptrr,",");
			value = strtok(NULL,", ");
			ind = strtol(index,NULL,16);  vl = strtol(value,NULL,16);
			identifier.push_back(ind);
			vals.push_back(vl);
			cout<<ind<<" - "<<vl<<endl;
		}
	}
	fclose(oar1);
	
}

Last edited by shabbir; 24Jun2009 at 08:59.. Reason: Code blocks
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
(1) Please use code blocks.
(2) The problem may not be in the code you've posted. Create a testbed that calls this from a simple main() function and make sure that reproduces the problem. If it does then post the new code here WITH CODE BLOCKS and we can have a look.

Out of interest, does the maximum line length of 16 characters include room for end of line and terminating NULL? So if you have the string "Hello\n" for example, that's actually 7 characters, and if you mean the "Hello" part is limited to 16 characters then you need larger arrays than just [16].
0
mmoose68's Avatar, Join Date: Jun 2009
Go4Expert Member
Sorry about that.

Code:
void * read_file(void*){
	FILE * oar1 ;
	oar1 = fopen("/dev/ttyT8S0","r");
	char *ptrr = new char[16] ;
	char *index = new char[16] ;
	char *value = new char[16] ;
	long ind = 0 ;
	long vl = 0 ;
	while(1){
		fgets(ptrr, 16, oar1);
		if(atoi(ptrr)!=0){
			index = strtok(ptrr,",");
			value = strtok(NULL,", ");
			ind = strtol(index,NULL,16);  vl = strtol(value,NULL,16);
			identifier.push_back(ind);
			vals.push_back(vl);
			cout<<ind<<" - "<<vl<<endl;
		}
	}
	fclose(oar1);
	
}
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Quote:
Originally Posted by mmoose68 View Post
Sorry about that.
No problem. How about point 2 and the thing about 16 byte strings, which you seem to have completely missed, or ignored.
0
mmoose68's Avatar, Join Date: Jun 2009
Go4Expert Member
Alright I did everything from point 2 you mentioned. Same result. The segfault happens after about 20 seconds of reading the file. Yes, 16 is plenty as the actual line is 14 chars.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
The memory leak could be the cause; you new[] three character arrays but you don't delete[] them. This isn't Java and you don't get a free garbage collector. I'm not sure why you want to use new/delete here anyway;
Code:
char ptrr[16];
is perfectly valid in this scenario, and since this is automatic you don't have to worry about cleanup, and this is likely a lot faster than digging around in the heap.
0
mmoose68's Avatar, Join Date: Jun 2009
Go4Expert Member
Alright, I have played with more options and now have this. It is still giving a seg fault
Code:
void * read_file(void*){
	FILE * oar1 ;
	oar1 = fopen("/dev/ttyT8S0","r");
	char ptrr[256] ;
	char *index = new char[256] ;
	char *value = new char[256] ;
	long ind = 0 ;
	long vl = 0 ;
	while(1){
		fgets(ptrr, 256, oar1);
		if(atoi(ptrr)!=0){
			index = strtok(ptrr,",");
			value = strtok(NULL,", ");
			ind = strtol(index,NULL,16);  vl = strtol(value,NULL,16);
			identifier.push_back(ind);
			vals.push_back(vl);
			cout<<ind<<" - "<<vl<<endl;
		}
	}
	fclose(oar1);
	
}
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Code:
char ptrr[256] ;
char *index = new char[256] ;
Not paying attention?
0
mmoose68's Avatar, Join Date: Jun 2009
Go4Expert Member
I tried changing the line to
Code:
char index[256] ;
and when I tried compiling, my strtok returns a char* not an array.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Whoops, sorry, turns out it's me not paying attention. So index and value don't need initialising at all, since they take on values returned by strtok. This also explains why you leak 512 bytes each time the function is called, which could be why you get a segfault.