我创建了一些Button,并且把指针存放在Tlist里面。
当我删除一个button的时候,我释放了button,也用pack,为什么count还是原来的数呢?下面是我查的delphi的帮助----------------------------------
Deletes all nil items from the Items array.procedure Pack;DescriptionCall Pack to move all non-nil items to the front of the Items array and reduce the Count property to the number of items actually used. Pack does not free up the memory used to store the nil pointers. To free up the memory for the unused entries removed by Pack, set the Capacity property to the new value of Count.文档里面非常详细的说了,用PACK可以把list里面非空的items排到前面,然后减少count的值,但不会释放空指针的空间,必须把capacity属性重新赋值为count。于是我的代码如下:
tmpButton:pointer;    TButton(tmpButton).Free;
    tmpButton:=nil;
    MyButtons.Pack;
    MyButtons.Capacity:=MyButtons.Count;    showmessage(IntToStr(MyCards.Count));可为什么我查看count的值,仍然是原来的值,而不是-1呢?
    showmessage(IntToStr(MyCards.Count));

解决方案 »

  1.   

    上面  showmessage(IntToStr(MyCards.Count));
    描述写错了。应该为 showmessage(IntToStr(MyButtons.Count));
      

  2.   

    TButton(tmpButton).Free;
        tmpButton:=nil;假设List[10]指向tmpButton,执行上面的代码后,List[10]指向一个非法地址,但并不为nil,因为你没有给List[10]赋空的操作。
      

  3.   

    你用 TObjectList 或者 TComponentList .TObjectList  在调用 Delete ,Remove ,Clear 或在释放TObjectList的时候都将销毁列表中的对象.TComponentList 的特别处在于如果链表中的一个组件在外部被释放的话,它将自动从TComponentList链表中删除.
      

  4.   

    to ppayun(~云彩) TButton(tmpButton).Free;//释放了指针所指的对象,
        tmpButton:=nil;//把指针指向空了。怎么说我的代码没有赋空的操作????