Phreak87 / LeptonicaSharp

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

Bitmap property on Pix class #25

Closed fdncred closed 6 years ago

fdncred commented 6 years ago

Can we please add a Bitmap property, with proper memory cleanup, to the Pix class? There really should be no need to always have to do a ToBitmap(). It should do that by default so when a user uses their Debugger Visualizer to view bitmaps, it's already there.

Phreak87 commented 6 years ago

Good idea.

Phreak87 commented 6 years ago

Implemented - but keep care! bitmap will be destroyed after 5 seconds.

fdncred commented 6 years ago

Having a bitmap property with a 5 second timeout is ridiculous, and it doesn't work anyway. Just have a property and clean it up on Dispose.

Let me add that, part of our problems within this project may be the difference between VB.Net and C#.

I noticed that your timer handler is a lambda, so I added to it and put a break point on the writeline. When is it supposed to be called? It never breaks into that line.

    ReadOnly Property Bitmap As Bitmap
        Get
            Dim BMP = Me.ToBitmap
            Dim TIM As New Timers.Timer(5000)
            AddHandler TIM.Elapsed, Sub()
                                        BMP.Dispose()
                                        Console.WriteLine("Disposing of temporary bitmap")
                                    End Sub
            TIM.Start()
            Return BMP
        End Get
    End Property
Phreak87 commented 6 years ago

thats funny. in my scenarios this works. Timer is a Lambda. after 5 Seconds the Lambda fires and disposes the bitmap. Maybe a exception occurs and is handled via the property and lambda ... => i delete that timing shit!

better idea is: if you open props 10 times the Backingbitmap will be destroyed and overwritten 10 times. Destroy on the pix destroys the BackingBitmap too. its the same time consumption as the timer struct but with less memory usage and cleanup after disposing. if the Pix changes the Bitmap changes too. (also via _all). For this to work i add additionally a Event "Disposing" to each class (with dispose functionality) to ensure the extension classes can handle the cleanup too. on pix cleanup the handler fires and all registred handlers will dispose the extension variables. => as i say ... different thinking - best of two worlds. i think this is the best way to handle each of the wishes.

Dim BackingBMP As Bitmap
ReadOnly Property Bitmap As Bitmap
    Get
        If Not IsNothing(BackingBMP) Then BackingBMP.Dispose()
        BackingBMP = Me.ToBitmap : Return BackingBMP
    End Get
End Property
Phreak87 commented 6 years ago

Plan Changed:

Now each Struct has a Caches Dictionary to handle disposables. Bitmap writes a entry as a disposable Value with its name. on replace the old will be disposed and deleted, on dispose all values from cache are beeing disposed/removed.

ReadOnly Property Bitmap As Bitmap
    Get
        If Caching.ContainsKey("Bitmap") Then Caching("Bitmap").dispose() : Caching.Remove("Bitmap")
        Caching.Add("Bitmap", Me.ToBitmap) : Return Caching("Bitmap")
    End Get
End Property

this is working