Board index » cppbuilder » OWLNext problem: TStringArray

OWLNext problem: TStringArray


2004-05-14 04:51:16 AM
cppbuilder53
Hi.
In my app, I use: TStringArray Stuff;
Whenever, I wish to get rid of an entry, I use
Stuff.Detach(x);
This works fine under OWL but, under OWLNext CodeGuard is reporting resource
leaks. If I use Stuff.Destroy(x) instead, the resource leaks seem to
disappear.
Should I always use Destroy instead of Detach -- even in OWL?
TIA
--
Marvin Avery
XXXX@XXXXX.COM
 
 

Re:OWLNext problem: TStringArray

Quote
In my app, I use: TStringArray Stuff;
Whenever, I wish to get rid of an entry, I use
Stuff.Detach(x);
This works fine under OWL but, under OWLNext CodeGuard is reporting
resource
leaks. If I use Stuff.Destroy(x) instead, the resource leaks seem to
disappear.

Should I always use Destroy instead of Detach -- even in OWL?
Depends. Detach() removes an item from a container without destroying
it. Thus you could continue using it, put it in another container,
etc.
If you want to have the container permanently dispose of the item, use
Destroy().
ASAIK, this is how OWLNext works as well.
Regards,
Bruce
 

Re:OWLNext problem: TStringArray

" Bruce Salzman" < XXXX@XXXXX.COM >wrote in message
Quote
>In my app, I use: TStringArray Stuff;
>Whenever, I wish to get rid of an entry, I use
>Stuff.Detach(x);
>This works fine under OWL but, under OWLNext CodeGuard is reporting
resource
>leaks. If I use Stuff.Destroy(x) instead, the resource leaks seem to
>disappear.
>
>Should I always use Destroy instead of Detach -- even in OWL?

Depends. Detach() removes an item from a container without destroying
it. Thus you could continue using it, put it in another container,
etc.

If you want to have the container permanently dispose of the item, use
Destroy().
Actually, I was thinking about this last night. I came to the conclusion
that it never makes sense to use Detach(). That is, if I have a TArray of
int's or long's, etc., Destroy() and Detach() do the same thing. In fact,
they are the same in the OWL source code. For "normal" objects, Detach()
leaves the object lying around but there is nothing referencing it because
doing something like Add(MyObject) adds a *copy* of the object to the array.
Sure, it is possible to get a pointer to this object in the array but that
is probably dangerous.
--
Marvin Avery
XXXX@XXXXX.COM
 

{smallsort}

Re:OWLNext problem: TStringArray

Quote
Actually, I was thinking about this last night. I came to the
conclusion
that it never makes sense to use Detach(). That is, if I have a
TArray of
int's or long's, etc., Destroy() and Detach() do the same thing. In
fact,
they are the same in the OWL source code. For "normal" objects,
Detach()
leaves the object lying around but there is nothing referencing it
because
doing something like Add(MyObject) adds a *copy* of the object to
the array.
Sure, it is possible to get a pointer to this object in the array
but that
is probably dangerous.
Right, for copyable objects you wouldn't notice the difference between
Detach() and Destroy(). The only times I have used Detach() were when
using the TI* containers, which manage object pointers. BTW, be aware
that if your program does use these indirect containers and you port
to the STL, you have a problem as the STL doesn't have anything
equivalent. You can solve this by wrapping object pointers in the
Boost library's shared_ptr before putting them in STL containers.
HTH,
Bruce
 

Re:OWLNext problem: TStringArray

" Bruce Salzman" < XXXX@XXXXX.COM >wrote in message
Quote


Right, for copyable objects you wouldn't notice the difference between
Detach() and Destroy(). The only times I have used Detach() were when
using the TI* containers, which manage object pointers. BTW, be aware
that if your program does use these indirect containers and you port
to the STL, you have a problem as the STL doesn't have anything
equivalent. You can solve this by wrapping object pointers in the
Boost library's shared_ptr before putting them in STL containers.

Thanks. It's nice to get a confirmation so I know I'm not making a mistake.
I'm not using any of the TI* containers so I think I'll just change all my
calls from Detach() to Destroy(). Eventually, I'm going to change all my
uses of BIDS to STL.
--
Marvin Avery
XXXX@XXXXX.COM