Seg Fault Mem Leak

Go4Expert Member
24Jun2009,03:46   #1
mmoose68's Avatar
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
Mentor
24Jun2009,04:24   #2
xpi0t0s's Avatar
(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].
Go4Expert Member
24Jun2009,04:27   #3
mmoose68's Avatar
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);
	
}
Mentor
24Jun2009,04:44   #4
xpi0t0s's Avatar
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.
Go4Expert Member
24Jun2009,04:55   #5
mmoose68's Avatar
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.
Mentor
24Jun2009,12:39   #6
xpi0t0s's Avatar
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.
Go4Expert Member
24Jun2009,20:57   #7
mmoose68's Avatar
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);
	
}
Mentor
24Jun2009,21:59   #8
xpi0t0s's Avatar
Code:
char ptrr[256] ;
char *index = new char[256] ;
Not paying attention?
Go4Expert Member
24Jun2009,22:09   #9
mmoose68's Avatar
I tried changing the line to
Code:
char index[256] ;
and when I tried compiling, my strtok returns a char* not an array.
Mentor
25Jun2009,00:36   #10
xpi0t0s's Avatar
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.