#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    7
    Rep Power
    0

    Delete an element from array of TBitmap


    I'd like to remove an element in an array of TBitmap. This means shifting successive elements to the left. This code works (mem is the index of the element to be deleted).
    Code:
    for i := mem to length(Images) - 2 do Images[i].Assign(Images[i+1]);
    Images[High(Images)].Free;
    SetLength(Images, High(Images));
    However, I think it could be more efficient if it just shifts the bitmap handles instead, therefore avoiding moving memory in the assign function. Both the following don't seem to work.
    Code:
    for i := mem to length(Images) - 2 do Images[i] := Images[i+1];
    Images[High(Images)].Free;
    SetLength(Images, High(Images));
    
    for i := mem to length(Images) - 2 do Images[i].Handle := Images[i+1].Handle;
    Images[High(Images)].Free;
    SetLength(Images, High(Images));
    They both get access violations when trying to access the bitmap at the index that was deleted. Any ideas? Thanks.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    7
    Rep Power
    0
    I think I've worked it out now actually. When freeing the last bitmap in the array, it also frees the penultimate one because they share the same pointer after shifting them. The correct way to do it is like this.
    Code:
    Images[mem].Free;
    for i := mem to length(Images) - 2 do Images[i] := Images[i+1];
    SetLength(Images, High(Images));

IMN logo majestic logo threadwatch logo seochat tools logo