Re: CList Concept



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
CObject myObject = new CObject();
as an example

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


.



Relevant Pages

  • Re: CList Concept
    ... there is a bunch of predefined collection classes for standard ... called automatically by the list class destructor. ... CObject myObject = new CObject; ... if I do it this way, will I get memory leak? ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: realloc in function
    ... My note about memory leak is also very important for proper use of realloc. ... > Keep in mind that assigning realloc result to the same pointer may lead ... > Why would you read Microsoft news from anywhere other then the MS news ... > " MVPs earn their status by being nominated by peers and Microsoft ...
    (microsoft.public.vc.language)
  • Re: question on malloc(0)
    ... the pointer returned by malloc, it's a memory leak regardless ... After the malloccall, ptr presumably ... points to some small allocated chunk of memory. ...
    (comp.lang.c)
  • Re: CList Concept
    ... Thanks for the advice. ... how do I destroy the elements in mylist? ... CObject myObject = new CObject; ... if I do it this way, will I get memory leak? ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: Parsing options in the same way they are passed to main
    ... > Jeff Rodriguez wrote: ... > This is also a guaranteed memory leak if realloc fails. ... > assign it to the destination pointer if it is non-null. ...
    (comp.lang.c)