Home > Software engineering >  Deposit CObject * C list, serialization to value, after the RemoveAt need to delete
Deposit CObject * C list, serialization to value, after the RemoveAt need to delete

Time:09-19

Deposit a CObject * type c + + list, list of values are serialized into value, when deleting the node, after the RemoveAt, still need to delete?
Such as a piece of code below

 
//definition of list
Typedef CTypedPtrList
CLnObjList m_objects;
CFile f;
Cstrings FilePath=F \ \ Project \ \ "CH. D";
CArchive Sama_ar (& amp; F, CArchive: : load);
M_objects. Serialize (Sama_ar);
F. lose ();
The POSITION pos=m_objects. GetHeadPosition ();
While (pos!=NULL) {
CLnObj * pObj=m_objects GetAt (pos);
M_objects. RemoveAt (pos);
The delete pObj;//I want to ask, there need
Pos=m_objects. GetHeadPosition ();
}

In the above code, want to know if you don't delete will not lead to memory leaks,
I sprout new, very thank you for the help of

CodePudding user response:

Key to see how you allocate insert operation, if it is to use new assigned must have the corresponding delete destruction

CodePudding user response:

Yes, yes, I know that if allocation when insert is new dynamic open space, the need to delete to delete the node, but if the list is through the serialization to read from the file value, in order to facilitate I stick the functions of the CObject serialization out (source code list storage is written by myself from CObject inherit a subclass of CLnObj, but in order to facilitate understanding I will write it as the CObject, so please don't tangle me how I can rewrite the CObject serialization function)
 
Void CLnObj: : Serialize (CArchive & amp; Ar)
{
int i;
The CObject: : Serialize (ar);
If (ar) IsStoring ()) {
Ar} else {
Ar> ID> AVNum> DVNum;
}
//these variables are of type int

CodePudding user response:

What would you add elements completely not influence, list the destructor is not to release the pointer, you don't release who release?

CodePudding user response:

List destructor after that is to say, you also need to manually release the pointer, if it is a new space, need to use the delete to release, then I want to ask, if it is serialized in the pointer (also known as a linked list of nodes), and use the delete to release this pointer space

CodePudding user response:

No difference, anyway the destructor will not help you to release,

You call the Serialize node still would you apply for memory,

CodePudding user response:

CLnObj * pObj
Don't need to delete
  • Related