TList > 不会 return from "delete src" where src is pointer to struct

TList > will not return from "delete src" where src is pointer to struct

我的环境:

C++ Builder XE4

我正在尝试使用 TList 来存储结构数据。以下是我的示例代码。

struct data_t {
    int key;
    String name;
};
TList *list;


void __fastcall TForm1::addButtonClick(TObject *Sender)
{
    // add records
    list = new TList();
    data_t *dataArray = new data_t[2];

    dataArray[0].key = 31415;
    dataArray[0].name = L"pi";
    list->Add(&dataArray[0]);

    dataArray[1].key = 27182;
    dataArray[1].name = L"napier";
    list->Add(&dataArray[1]);

    ShowMessage(L"added");
}
//---------------------------------------------------------------------------

void __fastcall TForm1::showButtonClick(TObject *Sender)
{
    // get records
    data_t *src;
    for(int idx=0; idx < list->Count; idx++) {
        src = (data_t*)list->Items[idx];
        String msg;
        msg = L"key:" + IntToStr(src->key) + " name:" + src->name;
        ShowMessage(msg);
    }

    // free
    for(int idx=0; idx < list->Count; idx++) {
        src = (data_t*)list->Items[idx];

        delete src; // will not return
    }
    delete list;
    list = NULL;
}
//--------------------------------------------------------------------------

以上代码的问题是代码不会 return in delete src.

没有delete src,代码执行到最后,但我认为我应该删除data_t赋值delete src

我想知道这个问题的解决方法,或者我犯的一些错误。

您存储在 TList 中的指针没有用 new 单独分配,所以不要尝试单独 delete 它们。您需要 delete[] 您分配的原始数组 new[]:

struct data_t {
    int key;
    String name;
};

TList *list = NULL;
data_t *dataArray = NULL;

void __fastcall TForm1::addButtonClick(TObject *Sender)
{
    // add records
    list = new TList();
    dataArray = new data_t[2];

    dataArray[0].key = 31415;
    dataArray[0].name = L"pi";
    list->Add(&dataArray[0]);

    dataArray[1].key = 27182;
    dataArray[1].name = L"napier";
    list->Add(&dataArray[1]);

    ShowMessage(L"added");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::showButtonClick(TObject *Sender)
{
    // get records
    data_t *src;
    for(int idx=0; idx < list->Count; idx++) {
        src = (data_t*)list->Items[idx];
        String msg;
        msg = L"key:" + IntToStr(src->key) + " name:" + src->name;
        ShowMessage(msg);
    }

    // free
    delete list;
    list = NULL;

    delete[] dataArray;
    dataArray = NULL;
}
//--------------------------------------------------------------------------

如果您想要 delete 单个项目,则必须单独 new 它们:

struct data_t {
    int key;
    String name;
};

TList *list = NULL;

void __fastcall TForm1::addButtonClick(TObject *Sender)
{
    // add records
    list = new TList();

    data_t *item = new data_t;    
    item->key = 31415;
    item->name = L"pi";
    list->Add(item);

    item = new data_t;
    item->key = 27182;
    item->name = L"napier";
    list->Add(item);

    ShowMessage(L"added");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::showButtonClick(TObject *Sender)
{
    // get records
    data_t *src;
    for(int idx=0; idx < list->Count; idx++) {
        src = (data_t*)list->Items[idx];
        String msg;
        msg = L"key:" + IntToStr(src->key) + " name:" + src->name;
        ShowMessage(msg);
    }

    // free

    for(int idx=0; idx < list->Count; idx++) {
        src = (data_t*)list->Items[idx];
        delete src;
    }

    delete list;
    list = NULL;
}
//--------------------------------------------------------------------------