Hi all:

I think I've done everything correctly, but my graphics program
still has a memory leak! Something tells me that this is the
suspicious module. I've looked at it again and again until my eyes
gloss over. Maybe someone else can pick up on the error. This module opens a long array of 24-bit pixel values from a device independent Bitmap (cDibMag).

VB Code:
  1. Public Function LongPixels() As Long()
  2. '_____________________________________________________
  3. '
  4. '   Purpose: converts the 2-dimensional 3/4 Byte DIB into a one-
  5. '       dimensional Long array, for easy summing of long int values.
  6. '       scans from left to right, then bottom to top.
  7. '       diblongarray(0) = lower left pixel.
  8. '       diblongarray(ubound) = upper right pixel.
  9. '   Assumptions: the DIB is 24-bit, the byte order of the pixels is
  10. '       r-g-b (not the common b-g-r)
  11. '   Affects:
  12. '   Inputs:
  13. '   Returns:
  14. '_____________________________________________________
  15.  
  16. Dim bDibMag() As Byte '2 dimensional byte array holds pixels by row,col
  17. Dim lX As Long, lY As Long 'x = pixel col, y = pixel row
  18. Dim saDibMag As SAFEARRAY2D
  19. Dim lXEnd As Long
  20. Dim l As Long
  21. Dim lCol As Long
  22.  
  23.     'structure the 2-dimensional safearray
  24.     With saDibMag
  25.         .cbElements = 1 'bytes to an element
  26.         .cDims = 2 'dimensions
  27.         .Bounds(0).lLbound = 0  'lbound of 1st dim
  28.         .Bounds(0).cElements = Height '# rows = height in pixels
  29.         .Bounds(1).lLbound = 0 'lbound of 2nd dim
  30.         .Bounds(1).cElements = BytesPerScanLine() 'calculate width with 24/32 row pad
  31.         .pvData = lPtrDibMag 'pointer to the DIBits
  32.     End With
  33.    
  34.     MoveMemory ByVal VarPtrArray(bDibMag()), VarPtr(saDibMag), 4   'copy safe array to bDibMag
  35.  
  36.     lXEnd = (Width - 1) * 3
  37.     l = 0
  38.  
  39.     ReDim lColor(Width * Height - 1)
  40.  
  41.     For lY = 0 To biDibMag.bmiHeader.biHeight - 1
  42.         For lX = 0 To lXEnd Step 3
  43.                 MoveMemory lCol, bDibMag(lX, lY), 3
  44.                 lColor(l) = lCol
  45.                 lCol = 0
  46.                 MoveMemory ByVal VarPtr(lCol), 0&, 3
  47.                 l = l + 1
  48.         Next lX
  49.     Next lY
  50.    
  51.     LongPixels = lColor
  52.    
  53.    
  54.     lPix = l 'store pixelcount
  55.    
  56.     'zero out the moved memory, so as to avoid
  57.     'a pacman-like memory-use situation
  58.     MoveMemory ByVal VarPtrArray(bDibMag), 0&, 4
  59.  
  60. End Function

Any ideas would be very welcome!

Thanks,