Menu

#19 OutOfMemoryException in call to WindowsTwaindsmDsmEntryUserinterface external method.

1.0
pending
Mark
2016-09-02
2016-01-19
No

The DatImagenativexferWindowsTwainDsm() method in the TWAIN class makes a call to WindowsTwaindsmDsmEntryImagenativexfer that make the amount of ram to increase a lot. I scan in duplex mode (about 30 to 50 sheets/papers) and I always run out of memory.

Additional Details:

The method WindowsTwaindsmDsmEntryImagenativexfer is an external method located in "twaindsm.dll" so I don't know how to proceed and fix it.

Discussion

  • Diego Torres

    Diego Torres - 2016-01-28

    After intensive research, I found the solution so you can scan without getting memory overflow or OutOfMemoryException. Basically we need to dispose (Bitmap) and free resources (IntPtr). Basically the fixes should be done in two methods of the TWAIN class. Last advise is: Do not store each retrieved bitmap in a list in memory because it also takes a lot of memory

    Class: TWAIN
    Method: NativeToBitmap(Platform a_platform, IntPtr a:intptrNative)
    Solution: Add these sentences after Bitmap bitmap = new Bitmap(bitmapStream);;

    // Cleanup...
    bitmapStream.Dispose();
    memorystream.Dispose();
    
    memorystream.Close();
    
    bitmapStream = null;
    memorystream = null;
    bBitmap = null;
    
    //D.T. 25/01/2015
    DsmMemFree(ref intptrNative);
    
    // Return our bitmap...
    DsmMemUnlock(a_intptrNative);
    return (bitmap);
    

    Class: TWAIN
    Method:STS DatImagenativexfer(DG a_dg, MSG a_msg, ref Bitmap a_bitmap)
    Solution: The if (sts == STS.XFERDONE) block should look like this:

    if (sts == STS.XFERDONE)
    {
        // Bump our state...
        m_state = STATE.S7;
    
        // Turn the DIB into a Bitmap object...
        // D.T. 25/01/2015 : This method also takes between 40 and 50 MB 
        // that needs to be cleaned.
    
        a_bitmap = NativeToBitmap(ms_platform, intptrBitmap);
    
        // We're done with the data we got from the driver...
    
        //Marshal.FreeHGlobal(intptrBitmap);
        //intptrBitmap = IntPtr.Zero;
    
        DsmMemFree(ref intptrBitmap);
    }
    

    I'll write a more detailed and explained article on this, however not sure when but I will.
    Thanks.
    Diego Torres

     
  • Mark

    Mark - 2016-09-02
    • status: open --> pending
    • assigned_to: Mark
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.