Results 1 to 5 of 5

Thread: FileSystemWatcher: Out Of Memory error

  1. #1

    Thread Starter
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577

    FileSystemWatcher: Out Of Memory error

    Hi,

    I have made a small application to monitor a directory. When I a new BMP file is added to that directory, my program converts it into a jpg file.

    I have achieved this by using the FileSystemWatcher class.

    Unfortunetely I recieved an 'Out Of Memory' error when a new bitmap file has been added to a folder.

    The problem code is below
    VB Code:
    1. Private Sub FileSystemWatcher1_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created
    2.         ListBox1.Items.Add(e.FullPath)
    3.  
    4.         Dim img As Image
    5.  
    6.         img = Image.FromFile(e.FullPath)
    7.  
    8.         'blah blah blah, do something
    9.  
    10.         img.Dispose()
    11.  
    12.     End Sub

    I recieve the error on this line
    VB Code:
    1. img = Image.FromFile(e.FullPath)

    It never happens the first time this sub is called, its always on the second or greater.

    Im out of ideas on what it could be, your help is much appreciated

    Thanks
    Last edited by Geespot; Apr 14th, 2004 at 06:12 AM.

  2. #2
    Hyperactive Member scuzymoto's Avatar
    Join Date
    Aug 1999
    Location
    Washington State
    Posts
    316
    I have had similar problems with the dispose method on images. Try adding a line right under the dispose line that says...

    img.image = nothing

    That cleared up some problems in my app that were related to memory. I wasn't running out of memory but it was complaining about errors related to memory. I don't remember the exact error.
    SCUZ

  3. #3
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Image.FromFile has an irky bug within its definition.


    Use Image.FromStream instead

    VB Code:
    1. Private Sub FileSystemWatcher1_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created
    2.         ListBox1.Items.Add(e.FullPath)
    3.  
    4.         Dim img As Image
    5.  
    6.         Dim fs As New System.IO.FileStream(e.FullPath,Read)
    7.          
    8.         img = Image.FromStream(fs)
    9.         fs.Close
    10.  
    11.         'blah blah blah, do something
    12.  
    13.         img.Dispose()
    14.         img =Nothing
    15.  
    16.     End Sub

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by scuzymoto
    I have had similar problems with the dispose method on images. Try adding a line right under the dispose line that says...

    img.image = nothing

    That cleared up some problems in my app that were related to memory. I wasn't running out of memory but it was complaining about errors related to memory. I don't remember the exact error.
    Dispose method has the effect of setting the object to null . So you don't have to set the object to null after disposal .

  5. #5

    Thread Starter
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577
    Thanks,
    Using a stream object did solve the Out Of Memory error, but has produced a new error.

    my new code is
    VB Code:
    1. '-- A new image as been added, increment counter, this has no purpose!
    2.         nThumbnailCount += 1
    3.  
    4.         '-- Get the filename of the new bitmap thats been added, without the BMP extension
    5.         Dim sFilename As String = e.FullPath.Substring(0, e.FullPath.Length - 4)
    6.  
    7.         '-- Add a JPG extension to the string
    8.         sFilename += ".jpg"
    9.  
    10.         '-- Copy the file, this is because you cant read a file when its in use
    11.         System.IO.File.Copy(e.FullPath, "__null.bmp", True)
    12.  
    13.         '-- Open the BMP file in memory
    14.         Dim oImg As Image
    15.  
    16.         '-- Read the image into a stream (this avoids the buggy .FromFile, and its faster too)
    17.         Dim fs As New IO.FileStream("__null.bmp", IO.FileMode.Open, IO.FileAccess.Read)
    18.         oImg = Image.FromStream(fs)
    19.  
    20.         '-- Immediately free up resources from stream, we have finished with it now
    21.         fs.Close()
    22.         fs = Nothing
    23.  
    24.         '-- Delete the copied file, dont need it now
    25.         System.IO.File.Delete("__null.bmp")
    26.  
    27.         '-- Save the image into the JPG file
    28.         'oImg.Save(sFilename, Imaging.ImageFormat.Jpeg)
    29.  
    30.         '-- Free up resources
    31.         oImg.Dispose()

    What Im doing is
    • Copying the bitmap into a temporary file
    • Opening the new copied file into a Stream
    • Loading the stream into the Image object
    • Freeing up the stream object
    • Deleting the copied bitmap file
    • Freeing up the Image object


    To replicate the conditions of the program, I am simply pasting (in windows explorer) a bmp file into the folder that the FileSystemWatcher is monitoring, which calls the above code

    After the second file that was pasted I get the following error

    The process cannot access the file "__null.bmp" because it is being used by another process.

    I have narrowed it down to this line
    VB Code:
    1. System.IO.File.Copy(e.FullPath, "__null.bmp", True)

    Which I dont understand because that file shouldnt exist because my code deletes it.

    I thought something like this would of been a walk in the park, guess I am over confident on such matters
    Last edited by Geespot; Apr 14th, 2004 at 06:15 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width