Phreak87 / LeptonicaSharp

Full featured wrapper for leptonica 1.77.0
Other
8 stars 5 forks source link

Memory leak #67

Open matthewl77 opened 2 years ago

matthewl77 commented 2 years ago

I think I found a bug in your library.

Pix.ToBitmap seemed not to be releasing memory. I was converting Pix to Bitmap in a loop and eventually led to out of memory error.

I traced it to pixWriteMemBmp (and pixWriteMemPng, and probably other pixWriteMemXXX functions)

        if (pfdataPtr != IntPtr.Zero) {
            Marshal.Copy(pfdataPtr, pfdataGen, 0, pfdataGen.Length);
        }

After copying the data, the pfdataPtr needs to be free()d, since it was malloc'ed in Natives.pixWriteMemBmp

I wrote a C# wrapper for the C free() function and added this:

        if (pfdataPtr != IntPtr.Zero) {
            Marshal.Copy(pfdataPtr, pfdataGen, 0, pfdataGen.Length);
                    c_free(pfdataPtr); // MY CODE
        }

That seemed to correct the problem. This might not be the best solution. I'm an amateur.