why "delete" by using TIniFile

I am just writing a program in c++ (Borland c++ builder)
In my program, i want to read from/write to an .ini file. I have some experience for using .ini file's in c++ but i was wondering about the different way's to use TIniFile.

For example, i have just tested two differend methods.

Methode 1

1
2
3
4
5
6
7
8
9
10
11
#include <inifiles.hpp>
#include "Unit1.h"
 
//---------------------------------------------------------------------------
void __fastcall TForm1::btnIniClick(TObject *Sender)
{
    TIniFile *MyIni = new TIniFile(ChangeFileExt("dir\\file", ".ini" ) );
    MyIni -> WriteString ("Section1", "Item1", "value");
    MyIni -> WriteInteger("Section2", "Item1", 35); 
    delete ini;// what will happen if I don't delete "ini" ?
}


Methode 2

1
2
3
4
5
6
7
8
9
10
#include <inifiles.hpp>
#include "Unit1.h"
 
TIniFile *MyIni = new TIniFile(ChangeFileExt("dir\\file", ".ini" ) );
//---------------------------------------------------------------------------
void __fastcall TForm1::btnIniClick(TObject *Sender)
{
    MyIni -> WriteString ( "Section1", "Item1", "value");
    MyIni -> WriteInteger("Section2", "Item1", 35); 
}


for sure, the difference between the two methods; method 1 is like all the example's. create/open -> write or read -> delete. all in the same function
method 2 create/open at start of program, somewhere else in the code write or read. never delete, and no need to create/open the file every time

i just checked if there is any difference by running the two different methods, but also by method 2 the program is closing the file after all read/write actions, and also by method 2, the program is not using more memory after each read/write action.

can someone tell me the reason for delete the ini each time?

Thanks in advance
I already get an answer somewhere else :)

for people who also had this question..

When you create a new item ( in this case, a TIniFile) you reserve some memory. with the function "delete", you set this memory part as free again. when you don't do this, you'll have a "memory leak".

for example, you create a TIniFile in a loop, then every loop there will be reserved another part of memory.

in my case, i only reserve a part of memory when i start the program, so i should delete it also when i close my program, else there is also a very little memory leak when i start my program again.
Topic archived. No new replies allowed.