Re: CList Concept
- From: Jimmy Lim <JimmyLim@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 27 Mar 2007 18:50:58 -0700
Thanks for the info, I have a better prespective now.
I find that a lot of List used by server client application uses a list of
pointer instead of a list of object. Any reason why or it is just
coincidental?
Sincerely
Jimmy
"voidcoder" wrote:
.
For a list of strings you just use CStringList (predefined class),
there is a bunch of predefined collection classes for standard
data types like strings etc.
You don't need to worry about freeing memory manually
when using list templates, unless it is a list of
pointers...
You can call RemoveAll() if you want, although it is
called automatically by the list class destructor. So
the only situation when you really need it is when
your list object was allocated dynamically:
{
CStringList *plist = new CStringList();
...
...
...
delete plist; // Destructor will release object list
}
or
{
CStringList list;
...
...
...
// Destructor will release object list
}
Jimmy Lim wrote:
Err... I just using
as an exampleCObject myObject = new CObject();
Thanks for the advice. I'm just wary of creating memory leaks.
Another question, if I declare the CList as followed
CList<CString, CString&> mylist;
and I have added a few elements inside.
When I exit my application, how do I destroy the elements in mylist? Do I
use the "RemoveAll()" method before I exit the application? if not, will it
cause a memory leak?
"voidcoder" wrote:
>> CObject myObject = new CObject();
I have serious doubts the following is compiling
at all. Is it?
I believe you don't need to worry about the list element
destruction in case with CList<CObject,CObject&> myList.
A new instance of object (a copy) is created every time
you insert a new object to the list, and is destroyed
when you remove it from the list. But you have to cope with
releasing the source object. If you use "new", then it
should be a corresponding "delete" somewhere. Or better
allocate instance on the stack, so that it is destroyed
automatically:
CList<CObject,CObject&> myList;
CObject myObject;
myList.AddHead(myObject);
Normally collection templates are used to handle your
own classes, but if you want CObjects, just use CObjList
then.
Alternatively, if you want a list of pointers to
object instances (not a list of object instances),
use CPtrList then. In this case you are responsible
to create and release objects added to the list.
Jimmy Lim wrote:
Hi All,
I have been using C for quite a long time so the object oriented concept
is still a bit alien to me. There fore I just want to clarify the concept of
the usage of CList.
Below is an example code:
//******************
CList<CObject,CObject&> myList;
CObject myObject = new CObject();
myList.AddHead(myObject);
CObject myObject2 = myList.RemoveHead();
//******************
Questions:
1. do I need to delete myObject after I have added it to myList?
2. if I do it this way, will I get memory leak?
What is the correct usage of CList, because I fear my way of implementation
might cause memory leak. I'm coding in a WinCE environment.
Please advice. Thanks
- References:
- Re: CList Concept
- From: voidcoder
- Re: CList Concept
- From: Jimmy Lim
- Re: CList Concept
- From: voidcoder
- Re: CList Concept
- Prev by Date: RE: How to detect memory leak and detect who causes a memory leak
- Next by Date: Re: Restart (reboot) system???
- Previous by thread: Re: CList Concept
- Next by thread: Database...
- Index(es):
Relevant Pages
|