 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Paul Guest
|
Posted: Fri May 12, 2006 7:16 pm Post subject: TThreadList Questions |
|
|
Hi,
Q1:
Spotted this snippet of code whilst reusing some code, and it suddenly
dawned on me this looked like an interview question I'd seen in the past
(for VC++). The question is, is this code correct? 'i' goes from zero to
count, and deletes the Items[i].
Can I confirm this deletes the object in the Item[i], but the list is
unchanged?
TList *Logs = SysLogs->LockList();
try
{
for(int i = 0; i < Logs->Count; ++i)
{
delete static_cast<TIdSysLog*>(Logs->Items[i]);
}
Logs->Clear();
}
__finally
{
SysLogs->UnlockList();
}
Q2:
This adds an object into the list, in a thread safe manner.
AClass* a= new AClass();
threadlist->Add(a);
Is it thread safe to remove an object from the list by
threadlist->Remove(a);
delete a;
Instead of doing:
TList *Logs = threadlist->LockList();
if ( Logs->Remove(a) > -1 )
{
delete a;
}
threadlist->UnlockList();
Paul |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Fri May 12, 2006 9:14 pm Post subject: Re: TThreadList Questions |
|
|
"Paul" <plmacca (AT) clara (DOT) co.uk> wrote in message
news:4464db64$1 (AT) newsgroups (DOT) borland.com...
| Quote: | is this code correct?
|
Yes.
| Quote: | 'i' goes from zero to count, and deletes the Items[i].
|
Correct.
| Quote: | Can I confirm this deletes the object in the Item[i], but the
list is unchanged?
|
The list is unchecked inside the loop. It is changed when Clear() is
called.
| Quote: | This adds an object into the list, in a thread safe manner.
AClass* a= new AClass();
threadlist->Add(a);
|
Yes.
| Quote: | Is it thread safe to remove an object from the list by
threadlist->Remove(a);
delete a;
|
Yes.
| Quote: | TList *Logs = threadlist->LockList();
if ( Logs->Remove(a) > -1 )
{
delete a;
}
threadlist->UnlockList();
|
That is essentially what TThreadList::Remove() does internally.
Gambit |
|
| Back to top |
|
 |
Roddy Pratt Guest
|
Posted: Fri May 12, 2006 10:14 pm Post subject: Re: TThreadList Questions |
|
|
"Paul" <plmacca (AT) clara (DOT) co.uk> wrote in message
news:4464db64$1 (AT) newsgroups (DOT) borland.com...
| Quote: | TList *Logs = SysLogs->LockList();
try
{
for(int i = 0; i < Logs->Count; ++i)
{
delete static_cast<TIdSysLog*>(Logs->Items[i]);
}
Logs->Clear();
}
__finally
{
SysLogs->UnlockList();
}
|
The try..finally is a bit of a thorny issue here. Yes, it guarantees that
the list is unlocked at the end, but think what could happen if an exception
is thrown anywhere in the 'try' section after the first 'delete' and before
the Clear() actually does its stuff. Your list won't have been cleared, but
it will contain pointers to freed memory.
Admittedly I can't see a scenario when this could happen , but it's
certainly not ideal.
- Roddy |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Fri May 12, 2006 11:14 pm Post subject: Re: TThreadList Questions |
|
|
"Roddy Pratt" <roddy at spam fritter dot com> wrote in message
news:44650718 (AT) newsgroups (DOT) borland.com...
| Quote: | The try..finally is a bit of a thorny issue here. Yes, it guarantees that
the list is unlocked at the end, but think what could happen if an
exception
is thrown anywhere in the 'try' section after the first 'delete' and
before
the Clear() actually does its stuff. Your list won't have been cleared,
but
it will contain pointers to freed memory.
|
To get around that, you would have to remove items from the list while you
are looping through it, ie:
TList *Logs = SysLogs->LockList();
try
{
while( Logs->Count > 0 )
{
TIdSysLog *Log = static_cast<TIdSysLog*>(Logs->Items[0]);
Logs->Delete(0);
delete Log;
}
}
__finally
{
SysLogs->UnlockList();
}
| Quote: | Admittedly I can't see a scenario when this could happen , but it's
certainly not ideal.
|
It could only happen if a destructor for an item throws an exception. Which
is illegal by the language standards, but can technically happen if the code
is buggy.
Gambit |
|
| Back to top |
|
 |
Paul Guest
|
Posted: Sat May 13, 2006 10:14 am Post subject: Re: TThreadList Questions |
|
|
Thanks for both your comments. I thought I was on the right track. It was
a bit hot yesterday in the office and my thought processes started to go
down hill.
| Quote: | TList *Logs = SysLogs->LockList();
try
{
while( Logs->Count > 0 )
{
TIdSysLog *Log = static_cast<TIdSysLog*>(Logs->Items[0]);
Logs->Delete(0);
delete Log;
}
}
__finally
{
SysLogs->UnlockList();
} |
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|