BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

TThreadList Questions

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage)
View previous topic :: View next topic  
Author Message
Paul
Guest





PostPosted: Fri May 12, 2006 7:16 pm    Post subject: TThreadList Questions Reply with quote



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





PostPosted: Fri May 12, 2006 9:14 pm    Post subject: Re: TThreadList Questions Reply with quote



"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





PostPosted: Fri May 12, 2006 10:14 pm    Post subject: Re: TThreadList Questions Reply with quote



"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





PostPosted: Fri May 12, 2006 11:14 pm    Post subject: Re: TThreadList Questions Reply with quote

"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





PostPosted: Sat May 13, 2006 10:14 am    Post subject: Re: TThreadList Questions Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.