twain-cs icon indicating copy to clipboard operation
twain-cs copied to clipboard

OutOfMemoryException in call to WindowsTwaindsmDsmEntryUserinterface external method. [sf#19]

Open kolomiets opened this issue 7 years ago • 2 comments

Reported by diegot1106 on 2016-01-19 16:57 UTC 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.

kolomiets avatar Apr 05 '17 11:04 kolomiets

Commented by diegot1106 on 2016-01-28 16:04 UTC 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

kolomiets avatar Apr 05 '17 11:04 kolomiets

Updated by mlmcl on 2016-09-02 18:23 UTC

  • status: open --> pending
  • assigned_to: Mark

kolomiets avatar Apr 05 '17 11:04 kolomiets