Results 1 to 6 of 6

Thread: Unable to free bitmap

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

    Exclamation Unable to free bitmap

    In this example I can't free bitmap ,because of error:
    Code:
    Error: Exception: Target bitmap was destroyed! at line 14
    Simba Code:
    program new;

    procedure test;
    var
      bmp ,w,h:integer;
      TPA : TPointArray;
    begin
       w := 10;
       h := 10;
       bmp := createbitmap(w,h);
       SetTargetBitmap(bmp);
       FindcolorsTolerance(TPA,0,0,0,w-1,h-1,0);

       FreeBitmap(bmp);
    end;

    begin
    test;
    end.

    ,so I have choice between getting error ,or to litter memory (The following bitmaps were not freed: [0]). What to do ?



    @Edit
    Simba Code:
    program new;

    procedure test;
    var
      bmp ,w,h:integer;
      TPA : TPointArray;
    begin
       w := 10;
       h := 10;
       bmp := createbitmap(w,h);
       SetTargetBitmap(bmp);
       FindcolorsTolerance(TPA,0,0,0,w-1,h-1,0);

       try
       FreeBitmap(bmp);
       except
       end;

    end;

    begin
    test;
    end.
    This construction doesn't generate any error. But ,I'm not sure ,is that really free bitmap in memory or just suppress warning messages in debug box?
    Last edited by bg5; 04-15-2012 at 05:54 PM.

  2. #2
    Join Date
    Jul 2008
    Location
    NSW, Australia
    Posts
    881
    Mentioned
    0 Post(s)
    Quoted
    3 Post(s)

    Wink

    Local Variable, It is stacked but not defined so it stay's in memory.
    It is corrupted when another function is called and adds something else to the stack so when trying you get an exception.

    Run this and you'll see what I mean:
    Simba Code:
    program new;

    procedure test;
    var
      bmp,w,h:integer;
      TPA : TPointArray;
    begin
       bmp := createbitmap(w,h);
       FreeBitmap(bmp);
       w := 10;
       h := 10;
       SetTargetBitmap(bmp);
       FindcolorsTolerance(TPA,0,0,0,w-1,h-1,0);
    end;

    begin
    test;
    end.

    Unfortunately we do not have static variables in Pascal besides Constant's and Global Variables.

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

    Default

    Well maybe you are right ,but honestly ,I have no idea what are you talking about :P

    You can make bmp as global variable ,problem will still exist.

    I thought the issue is ,that Simba is losing access to bitmap's handle after calling SetTargetBitmap...but this bitmap is still accessible and you can operate on it! Only thing you can't do is to free it. Weird:
    Simba Code:
    program new;
      {$i srl/srl.simba}
       {$I srl/srl/misc/debug.simba}

    var
    bmp:integer;

    procedure test;
    var
      w,h:integer;
      TPA : TPointArray;
    begin
       w := 30;
       h := 30;
       bmp := createbitmap(w,h);

       SetTargetBitmap(bmp);
       FindcolorsTolerance(TPA,0,0,0,w-1,h-1,0);
    end;

    begin
    test;
     invertbitmap(bmp);      // we can operate on bitmap (if bitmap is corrupted error should be here - but it's fine)
     debugbitmap(bmp);       // even show it!
       try
       freebitmap(bmp);
       except
       writeln('Error: Exception: Target bitmap was destroyed!');  // but not free!
       end;
    end.

    And the cause of problem is function SetTargetBitmap(bmp); ,if you comment it you won't get error.
    Last edited by bg5; 04-15-2012 at 08:53 PM.

  4. #4
    Join Date
    Jul 2008
    Location
    NSW, Australia
    Posts
    881
    Mentioned
    0 Post(s)
    Quoted
    3 Post(s)

    Default

    Sorry I forgot to mention it is actually freed,

    Run this and you'll see that it No longer Exists
    Simba Code:
    program new;

    procedure test;
    var
      w,h:integer;
      TPA : TPointArray;
    begin
       w := 10;
       h := 10;
       SetTargetBitmap(createbitmap(w,h));
       FindcolorsTolerance(TPA,0,0,0,w-1,h-1,0);
       try
       FreeBitmap(0);
       except
       Writeln('Destroyed');
       end;
       FreeBitmap(0);
    end;

    begin
    test;
    end.

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

    Default

    Just set the target back:

    Simba Code:
    program new;

    procedure test;
    var
      bmp ,w,h:integer;
      TPA : TPointArray;
    begin
       w := 10;
       h := 10;
       bmp := createbitmap(w,h);
       SetTargetBitmap(bmp);
       FindcolorsTolerance(TPA,0,0,0,w-1,h-1,0);
       SetDesktopAsClient;//or what ever your target was
       FreeBitmap(bmp);
    end;

    begin
    test;
    end.
    Working on: Tithe Farmer

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

    Default

    Run this and you'll see that it No longer Exists
    Run this

    Simba Code:
    program new;
      {$i srl/srl.simba}
    {$I srl/srl/misc/debug.simba}
    procedure test;
    var
      w,h:integer;
      TPA : TPointArray;
    begin
       w := 100;
       h := 100;
       SetTargetBitmap(createbitmap(w,h));
       FindcolorsTolerance(TPA,0,0,0,w-1,h-1,0);
       DrawTPABitmap(0,TPA,clgreen);
       DebugBitmap(0);
       try
       FreeBitmap(0);
       except
       Writeln('Destroyed');
       end;
       FreeBitmap(0);
    end;

    begin
    test;
    end.

    Just set the target back:
    Thx for advice ,I added SmartSetTarget; ,and it's fixed now

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
  •