Re: just a little stuck....
Could you be more specific? Why can't you add the object to a list?
Re: just a little stuck.... [Resolved]
ooops... sorry, just solved it!
Regards
John
Re: just a little stuck....
Quote:
Originally Posted by Vaderman
where MyMethod returns a singleton object. But what I would like to know is how do I add the object to a list before returning back?
John/Darth Vaderman, you really put me in darkness with this question. What list are you talking about?
Re: just a little stuck....
Apologies Cilu. I was basically trying to do too much (just trying to be flash with my code) and it wasn't working...
Just solved it and made it more simplier.. :)
Regards
John
Re: just a little stuck....
erm, one more question - which I suppose is not related but, hey ho here goes...
I have a class, and I've limited the number of instances on a class, similar to the design pattarn of a singleton and each instance created returns a new instance of the class on the heap as shown :
Code:
CDevice* CDevice::CreateInstance()
{
CDevice* ptrDevice = NULL;
if (m_nCounter < m_nMaxObjects)
{
ptrDevice = new CDevice;
ptrDevice->m_bDeviceStatus = FALSE;
ptrDevice->m_nDeviceType = DEVICE_RADIATOR;
ptrDevice->m_strDescription ="";
m_nCounter++;
}
return ptrDevice;
}
and to create an instance:
Code:
m_pDevice = CDevice::CreateInstance();
where m_pDevice is declared as a CDevice*.
Now, I've saved these instances in a vector as follows :
Code:
m_pvecDevice.push_back(m_pDevice);
This is where I'm having difficulties: How do I release the memory for each of these instances?
I attempt to release the memory as follows in the dtor() of another class:
Code:
while (it != m_pvecDevice.end())
{
m_pDevice = (CDevice*)*it;
// m_pDevice->Release(); // Results in stack overflow
delete m_pDevice; // can't do this - cannot access private member
it++;
}
In my Device Class, I have a Release() method, but it seems that it can't delete itself :
Code:
void CDevice::Release()
{
if (this != NULL)
{
delete this;
m_nCounter--;
}
}
as this produces a stack overflow - but i can't imagine why. :confused: Initially, I ensured that the m_nCounter flag was > 0 before deleting and then decrementing.
Can anyone pointer me in the right direction? :D
Regards
John
Re: just a little stuck....
Quote:
I attempt to release the memory as follows in the dtor() of another class:
Create a member function of CDevice that deletes the instances -- don't do this directly from another class. Make the other class call the CDevice member to delete the instance(s).
Not only will this work, but you now no longer write classes that needs to know how those objects were allocated in CDevice. It's CDevice that allocated the instances, CDevice should be the only one that knows how to destroy the instances.
Regards,
Paul McKenzie
Re: just a little stuck....
Also, I feel you are overcomplicating the deletion of the instances with Release, delete this, etc.
c
Just create a simple statiCDevice function, call it RemoveInstance(), that takes a pointer to an instance. All the CDevice::RemoveInstance() does is call delete on the passed-in pointer. Simple.
Regards,
Paul McKenzie