|
-
Nov 18th, 2002, 10:56 AM
#1
Thread Starter
Lively Member
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:
Public Function LongPixels() As Long()
'_____________________________________________________
'
' Purpose: converts the 2-dimensional 3/4 Byte DIB into a one-
' dimensional Long array, for easy summing of long int values.
' scans from left to right, then bottom to top.
' diblongarray(0) = lower left pixel.
' diblongarray(ubound) = upper right pixel.
' Assumptions: the DIB is 24-bit, the byte order of the pixels is
' r-g-b (not the common b-g-r)
' Affects:
' Inputs:
' Returns:
'_____________________________________________________
Dim bDibMag() As Byte '2 dimensional byte array holds pixels by row,col
Dim lX As Long, lY As Long 'x = pixel col, y = pixel row
Dim saDibMag As SAFEARRAY2D
Dim lXEnd As Long
Dim l As Long
Dim lCol As Long
'structure the 2-dimensional safearray
With saDibMag
.cbElements = 1 'bytes to an element
.cDims = 2 'dimensions
.Bounds(0).lLbound = 0 'lbound of 1st dim
.Bounds(0).cElements = Height '# rows = height in pixels
.Bounds(1).lLbound = 0 'lbound of 2nd dim
.Bounds(1).cElements = BytesPerScanLine() 'calculate width with 24/32 row pad
.pvData = lPtrDibMag 'pointer to the DIBits
End With
MoveMemory ByVal VarPtrArray(bDibMag()), VarPtr(saDibMag), 4 'copy safe array to bDibMag
lXEnd = (Width - 1) * 3
l = 0
ReDim lColor(Width * Height - 1)
For lY = 0 To biDibMag.bmiHeader.biHeight - 1
For lX = 0 To lXEnd Step 3
MoveMemory lCol, bDibMag(lX, lY), 3
lColor(l) = lCol
lCol = 0
MoveMemory ByVal VarPtr(lCol), 0&, 3
l = l + 1
Next lX
Next lY
LongPixels = lColor
lPix = l 'store pixelcount
'zero out the moved memory, so as to avoid
'a pacman-like memory-use situation
MoveMemory ByVal VarPtrArray(bDibMag), 0&, 4
End Function
Any ideas would be very welcome!
Thanks,
-
Nov 18th, 2002, 11:07 AM
#2
Addicted Member
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.
-
Nov 18th, 2002, 11:22 AM
#3
Thread Starter
Lively Member
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,
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|