Results 1 to 8 of 8

Thread: Handles ... and how to explain ?

  1. #1
    Join Date
    Oct 2011
    Posts
    805
    Mentioned
    21 Post(s)
    Quoted
    152 Post(s)

    Lightbulb Handles ... and how to explain ?

    I think you should compile this code to understand my problem ,because it's quite complex ,and I can't describe it clear:P

    Simba Code:
    program new;

    Function BmpToTIA(Bitmap_Handle : integer): T2dIntegerArray;
    Var
      I, J ,w ,h : Integer;
      B : TBitmap;
    Begin
      try
      B := TBitmap.Create;
      B.Handle := Bitmap_Handle;
      w := B.Width;
      writeln('inside function '+tostr(w));
      h := B.Height;
      SetLength(Result,w);
      For I:= 0 to w-1 Do
        SetLength(Result[I],h);
      For I := 0 to w-1 Do
        For J := 0 to h-1 Do
           Result[I][J] := B.Canvas.Pixels[I, J];
      B.Free;    // this line is evil
      except
      writeln('Exeption in BmpToTIA');
      end;
    End;

    var
     f,ff : T2dIntegerArray;
     bmp,bmp2,w,h ,a:integer;
     M : Tmufasabitmap;
     B_global : Tbitmap;
    begin
     GetClientDimensions(w,h);
      bmp := bitmapfromclient(1,1,w-1,h-1);
     M := GetMufasaBitmap(bmp);

     B_global := M.ToTBitmap;
     writeln('global :' +tostr(b_global.Width) +'    handle: ' +tostr(B_global.Handle));

     f :=  BmpToTIA(b_global.Handle );
     writeln('global :' +tostr(b_global.Width) +'    handle: ' +tostr(b_global.handle)+ '    - global object didn''t change ,but:');
     ff :=  BmpToTIA(b_global.Handle );
     writeln('It can''t be used by function any more!');
     M.Free;
    end.

    Output:

    Code:
    global :149    handle: 855972512
    inside function 149
    global :149    handle: 855972512    - global object didn't change ,but:
    inside function 0
    It can't be used by function any more!
    Basically what I have here:

    First i made Tbitmap object with image of client inside (B_global). Then I put it's handle to function ,which do stuff with it (in this case converts bitmap to T2dintegerarray ,but it's not important).
    Inside function I made new Tbitmap object (B) and assigned it to handle of global TBitmap. After all I add line B.Free; ,which should free local obcject and not affect global one.

    But ,what is very weird ,this line (B.Free do something ,that if I sent global object to this function second time it can't use it (see output).

    1. Anyone can explain this?

    2. If I create local object inside a function and don't free it ,it will be free automatically when function executed or i will have memory leak?

  2. #2
    Join Date
    Sep 2008
    Location
    Not here.
    Posts
    5,422
    Mentioned
    13 Post(s)
    Quoted
    242 Post(s)

    Default

    Since you use the actual handle of the bitmap, it frees it from simba's memory. I suggest using something like CopyBitmap. You actually aren't created a new object, you are accessing the object memory differently. /not-entirely-sure

  3. #3
    Join Date
    Oct 2011
    Posts
    805
    Mentioned
    21 Post(s)
    Quoted
    152 Post(s)

    Default

    Quote Originally Posted by mormonman View Post
    Since you use the actual handle of the bitmap, it frees it from simba's memory. I suggest using something like CopyBitmap. You actually aren't created a new object, you are accessing the object memory differently. /not-entirely-sure
    But look at output , I wrote that debug to check ,if global object still exist ...and it still exist:

    Code:
    global :149    handle: -771417308 // before function
    inside function 149                               // here is function
    global :149    handle: -771417308    - global object didn't change ,but: // after function's object was freed ,global still exist

  4. #4
    Join Date
    Sep 2008
    Location
    Not here.
    Posts
    5,422
    Mentioned
    13 Post(s)
    Quoted
    242 Post(s)

    Default

    I know this doesn't solve your problem, but why don't you just use GetBitmapAreaColors ?

  5. #5
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    I also want to know this. Will think about it. Though you know everytime you call TBitmap.Create you create a new handle... This might be a bit of memory hogging.
    Working on: Tithe Farmer

  6. #6
    Join Date
    Oct 2011
    Posts
    805
    Mentioned
    21 Post(s)
    Quoted
    152 Post(s)

    Default

    Quote Originally Posted by mormonman View Post
    I know this doesn't solve your problem, but why don't you just use GetBitmapAreaColors ?
    Because I didn't know this :P Anyway this function is only example ,the problem might concern many other functions.

  7. #7
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    Quote Originally Posted by beginner5 View Post
    Because I didn't know this :P Anyway this function is only example ,the problem might concern many other functions.
    Just don't use handles. I don't understand it either. But you will create a memory leak if you remove the b.Free; Simba does frees all local variables automatically, but not handlers. A TBitmap var is basically a handler. A suggest to use either this:

    Simba Code:
    Function BmpToTIA(Bitmap:Tbitmap): T2dIntegerArray;
    Var
      I, J ,w ,h : Integer;
    Begin
      w := Bitmap.Width;
      writeln('inside function '+tostr(w));
      h := Bitmap.Height;
      SetLength(Result,w);
      For I:= 0 to w-1 Do
        SetLength(Result[I],h);
      For I := 0 to w-1 Do
        For J := 0 to h-1 Do
           Result[I][J] := Bitmap.Canvas.Pixels[I, J];
    End;

    Or

    GetBitmapAreaColors
    Working on: Tithe Farmer

  8. #8
    Join Date
    Oct 2011
    Posts
    805
    Mentioned
    21 Post(s)
    Quoted
    152 Post(s)

    Default

    Hmm... But if you add line Bitmap.Free; you will see ,that global object was destroyed! So basically :
    Simba Code:
    Function BmpToTIA(Bitmap:Tbitmap): T2dIntegerArray;
    =
    Simba Code:
    Function BmpToTIA(var Bitmap:Tbitmap): T2dIntegerArray;
    :P It's not good ,when you don't want to touch global object.

    I didn't know this before too ,it looks like passing class object to functions is like passing pointer to this object ( like in java).
    Last edited by bg5; 04-26-2012 at 04:32 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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