How to Free Allocated Memory for DIBs
If you use the Document Imaging SDK and have a DIB you don’t need anymore, you should free the allocated memory for the DIB, if you don’t, your program has a memory leak. In this case you can just free the allocated memory with the GlobalFree API function.
But, if you use the ActiveX controls of the Document Imaging SDK, you can more easily free the allocated memory. The DropDIB method deallocates the memory of the DIB, and most Document Imaging controls have the DropDIB method, for example BiTiff.ocx, BiDib.ocx and BiDisp.ocx.
Example how to use the DropDib method in VB .NET
Dim hDibNew As Integer
Dim BiDibObj As Object
BiDibObj = CreateObject("BIDIB.BIDIBCtrl.1")
hDibNew = BiDibObj.RotateDIB90(hDib)
If hDibNew <> 0 Then
' Free old DIB. We don't need anymore
BiDibObj.DropDIB(hDib)
End If
But if you display a DIB (A) with the BiDisp control, and then you need to display a new DIB (B), you don’t have to call the DropDIB method for A. When the new hDib property of the BiDisp.ocx is set (BiDisp.hDib = B), the BiDisp.ocx frees the previous DIB internally and you don’t have to call the DropDIB method. If you simply want to free the DIB of BiDisp.ocx set the hDib propery to zero (BiDisp.hDib = 0). The BiDisp control will deallocate the memory of the displayed DIB automatically.
For example (in VB .NET)
Dim hDibNew As Integer
Dim BiDibObj As Object
Dim hDib As Integer = BiDisp.hDib
BiDibObj = CreateObject("BIDIB.BIDIBCtrl.1")
hDibNew = BiDibObj.RotateDIB90(hDib)
If hDibNew <> 0 Then
' Do not call DropDib for hDib.
' The BiDisp frees the allocated memory.
BiDisp.hDib = hDibNew
End If