Click here to Skip to main content
15,887,867 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi I have the following scenario.

1. A General class (call Aa);
2. A Global CList of Aa Class Pointers (typedef CList<aa*> AaList; AaList odaList);
3. A fun (call fun) Fills the odaList(please chech the code below);
4. Another function ( call fun2 ) deletes the CList Pointers(check the code);

C#
CWinApp theApp;
using namespace std;
//Class Aa Definition;
class Aa
{
public:
    INT nId;
    static INT nNextId;
    Aa();
    ~Aa(); 
    int a;
};
INT Aa::nNextId = 0;
Aa::Aa()
{
    nId = ++nNextId;
    printf("In Constructor: %x with Id = %d\n", this, nId);
}
Aa::~Aa()
{
    printf("In Destructor: %x\nwith Id = %d\n", this, nId);
}

//Aa List With Class Pointers
typedef CList<Aa*> aList; 
aList odaList;

//Fun Creates the List
int fun()
{
    Aa *o1 = new Aa();
    Aa *o2 = new Aa();
    Aa *o3 = new Aa();
    o1->a = 1;
    o2->a = 2;
    o3->a = 3;
    odaList.AddHead(o1);
    odaList.AddHead(o2);
    odaList.AddHead(o3);
    odaList.AddHead(o1);
    return 0;
}

//fun2 Deletes the List
int fun2()
{
    INT nCount = odaList.GetCount();
    for(INT i=0;i<nCount;i++)
    {
        POSITION pos = odaList.FindIndex(i);
        Aa *o1Temp = odaList.GetAt(pos);
        delete o1Temp;
    }
    odaList.RemoveAll();
    return 0;
}

//Main Function
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;
    fun();
    fun2();
    return nRetCode;
}


Now the problem is in fun2 while deleting the last element a runtime error is coming. May be because the "fun" have copied the same o1 object twice."fun2" don't know this. so how to overcome this problem.

You are right about the error. There are three (?) ways of handling this.

1. Create a depository list which only have the unique pointers in it, and delete the pointers from this list. Consider your odaList to just be a working set where duplicates are allowed.

or

2. When deleting, save every pointer in a map set, and check this map set if the pointer has been handled before deleting it.

or

3. Use a smart pointer in the list, which takes care of deletion for you when the list is destructed.

Depending on your situation, any of these three methods could be recommended.
 
Share this answer
 
v4
Comments
Olivier Levrey 1-Mar-11 7:50am    
Voted 5. This is the correct answer. I think that solution 1 is the easiest to implement.
The problem actually is that every time you delete an item from the list, the number of items remaining decreases. You should either delete in reverse order:
int fun2(){
    INT nCount = odaList.GetCount();
    for(INT i=nCount-1; i>=0; i--)
    {
        POSITION pos = odaList.FindIndex(i);
        Aa *o1Temp = odaList.GetAt(pos);
        delete o1Temp;
    }
    odaList.RemoveAll();
    return 0;
}
or always delete the top object:
int fun2(){
    INT nCount = odaList.GetCount();
    for(INT i=0; i<ncount;>    {
        POSITION pos = odaList.FindIndex(0);
        Aa *o1Temp = odaList.GetAt(pos);
        delete o1Temp;
    }
    odaList.RemoveAll();
    return 0;
}

Hope that helps.
 
Share this answer
 
Comments
Olivier Levrey 1-Mar-11 7:53am    
Voted 2.
Deleting the elements inside the list will not change its size. Only the RemoveXXX functions make the list decrease its size.
But your solution works if you delete every elements inside a loop using RemoveHead for example.
krmed 1-Mar-11 13:12pm    
It won't change the "size" but if you notice, he's using GetCount() - and that WILL change after a delete. Since the initial value of nCount is set to the original GetCount() result, he we be attempting to get elements past the end of what's available.

In his example, he added three elements and thefore nCount is set to 3 and i is 0, and the elements are 0, 1, and 2. After the first delete, i becomes 1 but the valid elements are now 0 and 1. The next time through the loop, i becomes 2, but the only valid index is now 1 - thus the problem.
Harrison H 1-Mar-11 17:56pm    
Oliver's right. Deleting the elements in the list won't change the size. Also, the OP was calling getCount() outside of the loop (maybe he reposted?). The real problem is he's deleting the same pointer twice.
Deleting the elements in the list won't change the size
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900