Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Seg Fault Mem Leak (http://www.go4expert.com/forums/seg-fault-mem-leak-t18162/)

mmoose68 24Jun2009 03:46

Seg Fault Mem Leak
 
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);
       
}


xpi0t0s 24Jun2009 04:24

Re: Seg Fault Mem Leak
 
(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].

mmoose68 24Jun2009 04:27

Re: Seg Fault Mem Leak
 
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);
       
}


xpi0t0s 24Jun2009 04:44

Re: Seg Fault Mem Leak
 
Quote:

Originally Posted by mmoose68 (Post 50354)
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.

mmoose68 24Jun2009 04:55

Re: Seg Fault Mem Leak
 
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.

xpi0t0s 24Jun2009 12:39

Re: Seg Fault Mem Leak
 
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.

mmoose68 24Jun2009 20:57

Re: Seg Fault Mem Leak
 
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);
       
}


xpi0t0s 24Jun2009 21:59

Re: Seg Fault Mem Leak
 
Code:

char ptrr[256] ;
char *index = new char[256] ;

Not paying attention?

mmoose68 24Jun2009 22:09

Re: Seg Fault Mem Leak
 
I tried changing the line to
Code:

char index[256] ;
and when I tried compiling, my strtok returns a char* not an array.

xpi0t0s 25Jun2009 00:36

Re: Seg Fault Mem Leak
 
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.


All times are GMT +5.5. The time now is 19:12.