Thread: Resouce Leak

Page 1 of 2 12 LastLast
Results 1 to 10 of 12
  1. #1 Resouce Leak 
    Registered Member
    Join Date
    Dec 2010
    Posts
    30
    Thanks given
    0
    Thanks received
    0
    Rep Power
    1
    I'm getting an error in eclipse saying that I have a resource leak and that 'objectFile' is not closed.

    Here is the boolean code:
    Code:
    	public boolean loadGlobalObjects(String fileName) {
    		String line = "";
    		String token = "";
    		String token2 = "";
    		String token2_2 = "";
    		String[] token3 = new String[10];
    		boolean EndOfFile = false;
    		BufferedReader objectFile = null;
    		try {
    			objectFile = new BufferedReader(new FileReader("./"+fileName));
    		} catch(FileNotFoundException fileex) {
    			Misc.println(fileName+": file not found.");
    			return false;
    		}
    		try {
    			line = objectFile.readLine();
    		} catch(IOException ioexception) {
    			Misc.println(fileName+": error loading file.");
    			return false;
    		}
    		while(EndOfFile == false && line != null) {
    			line = line.trim();
    			int spot = line.indexOf("=");
    			if (spot > -1) {
    				token = line.substring(0, spot);
    				token = token.trim();
    				token2 = line.substring(spot + 1);
    				token2 = token2.trim();
    				token2_2 = token2.replaceAll("\t\t", "\t");
    				token2_2 = token2_2.replaceAll("\t\t", "\t");
    				token2_2 = token2_2.replaceAll("\t\t", "\t");
    				token2_2 = token2_2.replaceAll("\t\t", "\t");
    				token2_2 = token2_2.replaceAll("\t\t", "\t");
    				token3 = token2_2.split("\t");
    				if (token.equals("object")) {
    					Objects object = new Objects(Integer.parseInt(token3[0]), Integer.parseInt(token3[1]), Integer.parseInt(token3[2]), Integer.parseInt(token3[3]), Integer.parseInt(token3[4]), Integer.parseInt(token3[5]), 0);
    					addObject(object);
    				}
    			} else {
    				if (line.equals("[ENDOFOBJECTLIST]")) {
    					try { objectFile.close(); } catch(IOException ioexception) { }
    					return true;
    				}
    			}
    			try {
    				line = objectFile.readLine();
    			} catch(IOException ioexception1) { EndOfFile = true; }
    		}
    		try { objectFile.close(); } catch(IOException ioexception) { }
    		return false;
    	}
    but the resource leak is here: (on the return true; statement)
    Code:
    		} else {
    				if (line.equals("[ENDOFOBJECTLIST]")) {
    					try { objectFile.close(); } catch(IOException ioexception) { }
    					return true;
    				}
    			}
    			try {
    Why am I getting this error?
    Reply With Quote  
     

  2. #2  
    Registered Member
    Join Date
    Dec 2011
    Posts
    454
    Thanks given
    17
    Thanks received
    37
    Rep Power
    61
    Make sure you close objectFile with
    objectFile.close();

    Here is an explanation of what a resource leak is:
    What is a resource leak?
    Reply With Quote  
     

  3. #3  
    Registered Member
    Join Date
    Sep 2014
    Age
    29
    Posts
    62
    Thanks given
    70
    Thanks received
    10
    Rep Power
    37
    Code:
    		try {
    			line = objectFile.readLine();
    		} catch(IOException ioexception) {
    			Misc.println(fileName+": error loading file.");
    			return false;
    		}
    You need to call objectFile.close(); in the catch block above.

    You may also want to rewrite that code. It's pretty terrible. (Don't blame yourself, blame whoever wrote this for PI)
    Reply With Quote  
     

  4. #4  
    Server Developer
    Argyros's Avatar
    Join Date
    Apr 2011
    Posts
    498
    Thanks given
    25
    Thanks received
    31
    Rep Power
    23
    Any time you are opening files or creating them you need to close them.
    Reply With Quote  
     

  5. #5  
    Registered Member
    Join Date
    Dec 2010
    Posts
    30
    Thanks given
    0
    Thanks received
    0
    Rep Power
    1
    Okay thanks for your help guys. But I recieve an error.
    I closed it here:
    Code:
    			line = objectFile.readLine();
    		} catch(IOException ioexception) {
    			Misc.println(fileName+": error loading file.");
    			objectFile.close();
    			return false;
    		}
    but now I get an error telling me:
    Code:
    Unhandled exception type IOException
    But if I change:
    Code:
    public boolean loadGlobalObjects(String fileName) {
    to:
    Code:
    public boolean loadGlobalObjects(String fileName) throws IOException {
    It gets rid of the error. But i don't know if this completely voids the whole code or not. Am I right by adding that it throws an exception? Thanks for your help.

    EDIT:
    Or I can add a try/catch statement and it also yields no errors.
    Code:
    			line = objectFile.readLine();
    		} catch(IOException ioexception) {
    			Misc.println(fileName+": error loading file.");
    			try {
    				objectFile.close();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			return false;
    		}
    So which one would be more right?
    Reply With Quote  
     

  6. #6  
    Server Developer
    Argyros's Avatar
    Join Date
    Apr 2011
    Posts
    498
    Thanks given
    25
    Thanks received
    31
    Rep Power
    23
    No having throws IOException is fine. You are supposed to have it in the method when dealing with I/O.
    Reply With Quote  
     

  7. #7  
    q.q


    Join Date
    Dec 2010
    Posts
    6,519
    Thanks given
    1,072
    Thanks received
    3,535
    Rep Power
    4752
    You should use a try-with-resource statement as it will fix the problem your having without even needing to worry about closing it

    The try-with-resources Statement (The Java™ Tutorials > Essential Classes > Exceptions)
    Reply With Quote  
     

  8. Thankful user:


  9. #8  
    Registered Member
    Join Date
    Dec 2010
    Posts
    30
    Thanks given
    0
    Thanks received
    0
    Rep Power
    1
    I can't seem to get the Try-With-Resource statement working properly.. it just yield a few errors as oppose to the IOException where there is only one error. And that is at the beginning of my objectHandler.

    Here is the code:
    Code:
    public ObjectHandler() {
    		loadGlobalObjects("./Data/cfg/global-objects.cfg");
    		loadDoorConfig("./Data/cfg/doors.cfg");
    	}
    the error is under the loadGlobalObjects and it says that its an unhandled exception type IOException. Now I can add a try/catch statement but I don't know what to write in the catch part(would it be to close the file, or the server all together?). When I just leave the catch statement empty, it fixes the error but I don't know if it properly loads the file and such. So how do I fix this properly? Thanks.


    Well I think I got the Try-With-Resource statement working properly. Can you tell me if these two statements are basically the same?
    Here is my try with resource statement:
    Code:
    	    try {
    	    	line = objectFile.readLine();
    	    } finally {
    	    	Misc.println(fileName+": error loading file.");
    	    	objectFile.close();
    	    }
    	}
    Here it is with IOException:
    Code:
    try {
    			line = objectFile.readLine();
    		} catch(IOException ioexception) {
    			Misc.println(fileName+": error loading file.");
    			return false;
    		}
    And if my try with resource statement happens to be correct, it still brings me this error:
    Code:
    unhandled exception type IOException
    for the statement: (which is in the beginning of the class)
    Code:
    loadGlobalObjects("./Data/cfg/global-objects.cfg");
    So how do I fix this error?

    P.S.
    I'm searching everywhere for help with this and you guys have helped me with it the most so far. Thanks!
    Reply With Quote  
     

  10. #9  
    q.q


    Join Date
    Dec 2010
    Posts
    6,519
    Thanks given
    1,072
    Thanks received
    3,535
    Rep Power
    4752
    try this

    Code:
    public boolean loadGlobalObjects(String fileName) {
        String line = "";
        String token = "";
        String token2 = "";
        String token2_2 = "";
        try (BufferedReader objectFile = new BufferedReader(new FileReader("./"+fileName))) {
            line = objectFile.readLine();
            while(line != null) {
                line = line.trim();
                int spot = line.indexOf("=");
                if (spot > -1) {
                    token = line.substring(0, spot).trim();
                    token2 = line.substring(spot + 1).trim();
                    token2_2 = token2.replaceAll("\t\t", "\t");
                    token3 = token2_2.split("\t");
                    if (token.equals("object")) {
                        Objects object = new Objects(Integer.parseInt(token3[0]), Integer.parseInt(token3[1]), Integer.parseInt(token3[2]), Integer.parseInt(token3[3]), Integer.parseInt(token3[4]), Integer.parseInt(token3[5]), 0);
                        addObject(object);
                    }
                }
                line = objectFile.readLine();
            }
        } catch(FileNotFoundException fileex) {
            Misc.println(fileName+": file not found.");
            return false;
        } catch(IOException ioexception) {
            Misc.println(fileName+": ioexception.");
            return false;
        }
        return false;
    }
    Reply With Quote  
     

  11. #10  
    Registered Member
    Join Date
    Dec 2010
    Posts
    30
    Thanks given
    0
    Thanks received
    0
    Rep Power
    1
    Well, I placed that in. And there is only one error and that's because objectFile is declared twice in the method. Should I get rid of the bufferedreader objectFile = null? or something else?
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. [LEAK] unreleased Picture of New High Detail
    By I.Say.Chris in forum Media
    Replies: 8
    Last Post: 07-10-2008, 09:05 AM
  2. leaked irl zezima pics
    By Balrog in forum Images
    Replies: 14
    Last Post: 04-13-2008, 02:37 AM
  3. RuneEmu - Leaked
    By Cowie` in forum Downloads
    Replies: 35
    Last Post: 01-02-2008, 06:38 PM
  4. ReenScapeShift Tables, 'leaked' By Impulse-X
    By Impulse-X in forum RS2 Server
    Replies: 3
    Last Post: 09-03-2007, 09:13 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •