Results 1 to 3 of 3

Thread: Help finding memory leak

  1. #1

    Thread Starter
    Lively Member HeVa's Avatar
    Join Date
    Jul 2001
    Location
    DC
    Posts
    115

    Help finding memory leak

    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,
    H e*V a

  2. #2
    Addicted Member
    Join Date
    Aug 2002
    Posts
    192
    MoveMemory ByVal VarPtr(lCol), 0&, 3

    I think it should read , 4.



    MoveMemory ByVal VarPtrArray(bDibMag()), VarPtr(saDibMag), 4

    bDibMag() should read bDibMag
    Last edited by dafhi; Nov 18th, 2002 at 11:13 AM.

  3. #3

    Thread Starter
    Lively Member HeVa's Avatar
    Join Date
    Jul 2001
    Location
    DC
    Posts
    115
    dafhi:

    Thanks very much for your response.

    Maybe I'm missing something, but I don't think that's it. I have
    two blocks of memory that I'm copying ("moving"): the 4-byte
    block and the 3-byte block. The 3-byte block is processed and
    cleaned up in the loop, where it is converted to a long value.
    The 4-byte block gets the byte array out of the DIB. Because
    of the mismatch between the 24-bit length of the pixel values
    and the 32-bit length of the long integer array, if I change the
    3 to a 4 as you suggest, the array will read in the wrong values.
    Example:

    Here's what I need:
    1st pixel long = 1st byte & 2nd byte & 3rd byte & empty
    2nd pixel = 4th - 5th - 6th - empty
    3rd pixel = 7th - 8th - 9th - empty
    and so on ...

    Here's what I believe I would get if I made your change:
    1st pixel = 1st-2nd-3rd-4th
    2nd pixel = 5th-6th-7th-8th
    3rd pixel = 9th-10th-11th-12th
    and so on...

    Do you agree, or am I missing something?

    Thanks Again so very much,
    H e*V a

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width