too many files opened + segmentation fault

This is a discussion on too many files opened + segmentation fault within the C Programming forums, part of the General Programming Boards category; Code: FILE *aFile; void readfile() { if ((aFile = fopen("filename","r")) == NULL) { perror("error"); } while(fgets(line,sizeof(line),sFile)) { // do the ...

  1. #1
    kfc
    kfc is offline
    Registered User
    Join Date
    Sep 2006
    Posts
    7

    too many files opened + segmentation fault = socket close issue

    Code:
    FILE *aFile;
    
    
    void readfile() {
    
    if ((aFile = fopen("filename","r")) == NULL)
    		{
    			perror("error");
    		}
    while(fgets(line,sizeof(line),sFile))
    		{
    // do the work, just grabbing strtoks
    
    }
    
    fclose(aFile);
    }
    I run this as part of my app, but since introducing it, I've caused this error to pop up after exactly 1 hour of running:

    too many files opened
    segmentation fault


    It used to happen much quicker when I had
    Code:
    FILE *aFile;
    within the function. I'm guessing because it ran out of file descriptors.

    But, right now I see no reason/excuse for it to run out of file descriptors, I'm properly closing the file descriptor, ...right?
    Last edited by kfc; 09-03-2006 at 12:58 PM. Reason: problem solved

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    > if ((aFile = fopen("filename","r")) == NULL)

    > while(fgets(line,sizeof(line),sFile))

    Are you sure you're opening and closing the right files?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    kfc
    kfc is offline
    Registered User
    Join Date
    Sep 2006
    Posts
    7

    Red face

    Quote Originally Posted by Salem
    > if ((aFile = fopen("filename","r")) == NULL)

    > while(fgets(line,sizeof(line),sFile))

    Are you sure you're opening and closing the right files?
    sorry, that's my second function

    the only function being run does indeed use the same file names.

    however I do have a second function that's never called and has different names, that wouldn't affect it right.


    More information:

    All of this is typed within "main", and I'm including:
    #include <stdio.h>
    #include <sys/socket.h>
    #include <arpa/inet.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/signal.h>

    I just commented out my second function a few minutes ago, since I'm getting paranoid over what could be the cause. And we'll see how long my app lasts.
    Last edited by kfc; 09-02-2006 at 04:05 PM.

  4. #4
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    all of this is typed within main? do you mean the readfile function is declared within main? if it is, it shouldn't be. write the readfile function before you call main, and call it in main.
    Last edited by Bleech; 09-02-2006 at 04:20 PM.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    You've snipped out way too much information to even make a vague guess possible.

    Snip out code which isn't run, then snip out code which is run but which doesn't stop it crashing.

    If you don't see the problem, then hopefully you'll end up with a small program which crashes which you can post here complete.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    kfc
    kfc is offline
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    ok i will work on cleaning up my code first and seeing what crashes it removing bit by bit

    yes, the functions were everywhere and some inside main

    I've discovered fprintf being called as well, you think that could cause it? It's not even opening a file.

    fprintf(stderr,"Error\n",buffer);

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    Absolutely anything could be causing it, and the real cause doesn't have to be anywhere near where the problem is being reported.

    Post code which crashes, there is no way to tell what could be the problem, there's simply too many ways to mess up.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,530
    I suggest providing a small but complete example that exhibits your problem.

    Without that, we'll just be guessing about the cause.

    Lacking such information, some obvious guesses are

    1) Some other code is molesting a pointer, and the crash is a side-effect of that (even if the pointer molestation is in code with no obvious relationship to the code which encounters the problem)

    2) In some circumstances, your code is returning from your function, or forgetting to close a file.

    3) Your code is (in your comments) "just grabbing strtoks" is storing a pointer somewhere where it is used later, when it has become a dangling reference.

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    11
    If you get "Too many open files" and you have a function that open a file....
    could be posible you call this function from a recursive function elsewhere ?
    are you sure you don't get out from the function (trough a return in some part) without actually closing the file ?

    And, always try to avoid global variables if no strong reason. Declare aFile an sFile within the function

    -------------
    http://www.uberum.com

  10. #10
    kfc
    kfc is offline
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    just updating the situation, letting u guys know i didn't commit suicide over it

    interesting tidbits as I pair the program down (as suggested):

    -if I pair it down to the readfile function, it NEVER dies (this is why I'm taking so long to pair it down, I have no idea what function is doing it)

    -it can run 2550 times before it dies, EXACTLY, when all functions are included. this is consistent and happens every time. I'm guessing this is my OS's file descriptor limit, correct?

    -I will begin searching for the offendors, they are likely opening the file somewhere else in the app!


    edit: Found her! the offending function, finally. I will paste when I cut it down to the offending code now.
    Last edited by kfc; 09-03-2006 at 10:34 AM.

  11. #11
    kfc
    kfc is offline
    Registered User
    Join Date
    Sep 2006
    Posts
    7

    Red face

    you guys will never believe this, here's the offending code:

    Code:
    			
    if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
    	perror("failed");
            close(sock);
    }
    
    if ((connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr))) < 0) {
               printf("failed");
               close(sock);
    }



    I changed them to this (BOLD = parts I changed, nothing else):


    Code:
    			
    if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
            close(sock);
    	perror("failed");
    }
    
    if ((connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr))) < 0) {
               close(sock);
               printf("failed");
    }

    Problem solved! now it runs forever. Obviously it wasn't closing the socket, it was just printing the errors. Somehow perror/printf stop a close from happening. Isn't that weird? or am I a noob? Or perhaps I'm not even connecting properly, criticism is fully welcome.

    Trying to learn sockets without schooling is hard.


    I want to thank everyone for your helps, I wouldn't have found it otherwise. You guys made me believe it wasn't what the error was reporting (open files) and that in fact it could be anywhere in my program. I then had hallucinations describing to me that sockets are just like files and that if too many open it could produce the same error as fopen.
    Last edited by kfc; 09-03-2006 at 01:00 PM.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    Well the second one is surely necessary, but the first isn't.
    If it wasn't created successfully, then there is nothing to close.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    kfc
    kfc is offline
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    Quote Originally Posted by Salem
    Well the second one is surely necessary, but the first isn't.
    If it wasn't created successfully, then there is nothing to close.
    i'll give that a shot and see what happens

    i added them at the same time, so you're likely right that the second one was the contributing factor

    I'm just curious why the printf/perror could abort my close?

    edit: I dont have copies of my code from before, so it's possible I added the close and there was none. investigating..


    edit2: oh! i found it. here is the real reason:

    Code:
    if ((connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr))) < 0) {
    sock = 0;
               close(sock);
               printf("failed");
    }

    closing a socket of value 0 caused the issue, it was removed when i swapped lines around.
    Last edited by kfc; 09-03-2006 at 03:04 PM.

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    I can see that even if you do this
    > if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0)
    Then do this
    > close(sock);

    Your code incorrectly goes on to do this
    > if ((connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr))) < 0)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    kfc
    kfc is offline
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    Quote Originally Posted by Salem
    I can see that even if you do this
    > if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0)
    Then do this
    > close(sock);

    Your code incorrectly goes on to do this
    > if ((connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr))) < 0)
    I put an else now before it, so it's like:
    Code:
    if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
    	perror("failed");
    }
    else {
             if ((connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr))) < 0) {
                   printf("failed");
                   close(sock);
             }
    }
    that should do it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-29-2008, 12:11 AM
  2. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  3. Why am I getting segmentation fault on this?
    By arya6000 in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2008, 07:32 AM
  4. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 03:01 AM
  5. debugging: sigsegv, segmentation fault???
    By Gonzo in forum C Programming
    Replies: 9
    Last Post: 09-16-2003, 07:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21