shadowrecon Posted April 1, 2012 Share Posted April 1, 2012 In Map.Scar, you are creating a bmp and free it at the end but you have an exit statement between them. so if the exit state was true then the bmp would be left in the memory. eventually causing a error. [scar] {=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= function FindMMDotsExOverlap(ColorDiffs: TIntegerArray): T2DPointArray; Contributors: Wanted Description: Finds all MMDots you specify i.e. cldWhiteDot etc. see Global.scar This one uses overlapping checking, so it's twice as slow. Date Created: December 8th, 2011. By Wanted. RS2 Build 688. Last Modification: February 19th, 2012. By Wanted. RS2 Build 701. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=} function FindMMDotsExOverlap(ColorDiffs: TIntegerArray): T2DPointArray; var I, II, H, HH, Map, DC, Wi, He, C, O: Integer; Offset: T2DPointArray; TPA: TPointArray; L: TIntegerArray; B: Boolean; begin Result := [TPointArray([])]; HH := High(ColorDiffs); SetLength(Result, HH + 1); SetLength(L, HH + 1); if (HH < 0 ) then Exit; Map := BitmapFromString(MMX2 - MMX1 + 4, MMY2 - MMY1 + 4, ''); CopyClientToBitmap(Map, MMX1 - 2, MMY1 - 2, MMX2 + 2, MMY2 + 2); DC := GetClientCanvas.Handle; SetTargetDC(GetBitmapDC(Map)); GetBitmapSize(Map, Wi, He); FindColors(TPA, 65536, 3, 3, Wi - 3, He - 3); FilterPointsDist(TPA, 0, MMRDe, (Wi / 2) - 2, (He / 2) - 2); SetTargetDC(DC); H := High(TPA); if (H < 0) then Exit; Offset := [TPointArray([Point(-2, 0), Point(-1, 0)]), TPointArray([Point(0, -2), Point(0, -1)])]; for I := 0 to H do for O := 0 to 1 do begin C := (FastGetPixel(Map, TPA.X + Offset[O][0].X, TPA.Y + Offset[O][0].Y) - FastGetPixel(Map, TPA.X + Offset[O][1].X, TPA.Y + Offset[O][1].Y)); B := False; for II := 0 to HH do if (C = ColorDiffs[iI]) then begin B := True; Inc(L[iI]); SetLength(Result[iI], L[iI]); Result[iI][L[iI] - 1] := Point(TPA.X + MMX1 - 4, TPA.Y + MMY1 - 4); Break; end; if (B) then Break; end; FreeBitmap(Map); end; {=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= function FindMMDotsEx(ColorDiffs: TIntegerArray): T2DPointArray; Contributors: Wanted Description: Finds all MMDots you specify i.e. cldWhiteDot etc. see Global.scar Date Created: December 8th, 2011. By Wanted. RS2 Build 688. Last Modification: February 19th, 2012. By Wanted. RS2 Build 701. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=} function FindMMDotsEx(ColorDiffs: TIntegerArray): T2DPointArray; var I, II, H, HH, Map, DC, Wi, He, C: Integer; L: TIntegerArray; TPA: TPointArray; begin Result := [TPointArray([])]; HH := High(ColorDiffs); SetLength(Result, HH + 1); SetLength(L, HH + 1); if (HH < 0 ) then Exit; Map := BitmapFromString(MMX2 - MMX1 + 4, MMY2 - MMY1 + 4, ''); CopyClientToBitmap(Map, MMX1 - 2, MMY1 - 2, MMX2 + 2, MMY2 + 2); DC := GetClientCanvas.Handle; SetTargetDC(GetBitmapDC(Map)); GetBitmapSize(Map, Wi, He); FindColors(TPA, 65536, 3, 3, Wi - 3, He - 3); FilterPointsDist(TPA, 0, MMRDe, (Wi / 2) - 2, (He / 2) - 2); SetTargetDC(DC); H := High(TPA); if (H < 0) then Exit; // <- But you could exit here. for I := 0 to H do begin C := (FastGetPixel(Map, TPA.X - 2, TPA.Y) - FastGetPixel(Map, TPA.X - 1, TPA.Y)); for II := 0 to HH do if (C = ColorDiffs[iI]) then begin Inc(L[iI]); SetLength(Result[iI], L[iI]); Result[iI][L[iI] - 1] := Point(TPA.X + MMX1 - 4, TPA.Y + MMY1 - 4); Break; end; end; FreeBitmap(Map); // <--- Free'ed here end; [/scar] Quote Link to comment Share on other sites More sharing options...
FHannes Posted April 1, 2012 Share Posted April 1, 2012 The proper way of doing this would actually be [scar]Map := BitmapFromString(MMX2 - MMX1 + 4, MMY2 - MMY1 + 4, ''); try // All of the code finally FreeBitmap(Map); end;[/scar] Using try...finally, the finally section would be called when you exit the try section, even if you call Exit to get out of the function. Quote Link to comment Share on other sites More sharing options...
Wanted Posted April 1, 2012 Share Posted April 1, 2012 https://github.com/OSI1/OSI1/commit/792a55c4b8d8e9ac3e9ee6d878b10e69cf5c2ab6 Good find That's also a really useful piece of information that try except finally works in that way, however I find it unnecessary at this time since the code shouldn't throw any errors anymore. Definitively going to use it in the future though. Quote Link to comment Share on other sites More sharing options...