Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kolomiets opened this issue Apr 5, 2017 · 2 comments
Milestone

Comments

@kolomiets
Copy link
Contributor

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 kolomiets added this to the 1.0 milestone Apr 5, 2017
@kolomiets kolomiets changed the title OutOfMemoryException in call to WindowsTwaindsmDsmEntryUserinterface external method. OutOfMemoryException in call to WindowsTwaindsmDsmEntryUserinterface external method. [sf#19] Apr 5, 2017
@kolomiets
Copy link
Contributor Author

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
Copy link
Contributor Author

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant