Is there any flaw in this complex class using RPointerArray?
| Wed, 2008-03-26 05:42 | |
class CFamily : public CBase---------------------------------------------------- I design this class for challenge the useage of RPointerArray ,and i doubt the difference between CleanupClosePushL and CleanupResetAndDestroy::PushL,who can figure it out?
|
|






,and i doubt the difference between CleanupClosePushL and CleanupResetAndDestroy::PushL,who can figure it out?
Forum posts: 2006
There are at least many flaws in your code. You should really consider practicing a bit more before thinking about flaws in system implementation (btw, could you mention what flaws do you see in RPointerArray ? ):
- RPointerArray are R-classes. You do not need to allocate them with new(ELeave).
- you should NEVER put class data members on the cleanup stack
Eric Bustarret
NewLC Founder & CEO / Professional Symbian OS Consultant
Forum posts: 23
Oh,My God .
I 'm sorry and actually make these mistakes.
U are right ,Eric. There is no need to allocate R-class with new(ELeave) and yet the Kernel resources always need to protected .
As far as i know Kernel resources used by the R-objects like RPointerArray are need protected via CleanupClosePushL.
Then how can we avoid the resoures unclose when exception happend. The array is member of a class ,we cannot push the array to the cleanup stack??
PS:Of course there is no flaw in system implementation , i just doubt the useage of mine.
Eric.Pen
Forum posts: 2006
You are right. However in your case this is the CFamily object that you need to put on cleanup stack (unless it is itself a data member of another class). Deleting the object (either by calling delete or PopAndDestroy) will call its destructor and this is where you just need to call RPointerArray::Reset or RPointerArray::ResetAndDestroy to clean the array.
Eric Bustarret
NewLC Founder & CEO / Professional Symbian OS Consultant
Forum posts: 159
And find out what array granularity is for.
Forum posts: 23
Ok, thing seems to be clear , AND i really understand where is my fault ! I should not put the member data of class to the cleanup stack , because the class itself is in charge of member's destruction. What am i doing in this wrong case is: put the member data to the cleanupstack and then new a C-object of CFamily , finally push this object to the stack ,
All action is just in disorder.
By the way , when the element insertion causes the array to re-allocated , the granularity is the length to increase .
Whatever ,thanks for the eric's help ! Thanks !
Forum posts: 672
Also, NewL and NewLC are the wrong way around -- NewLC should leave object on the cleanup stack, NewL should not.
Edit: Please learn to use the naming conventions too. Knowing, by the name of the variable, if it is a member variable or not, is a very valuable help. As you have now learned from the R class usage too.
Forum posts: 159
Yes that is what the granularity is for.
So therefore it is a bad idea to have a granulatory of 1 if your array is going to store more than a handful of items. i.e. if the array will store 500 items, then every time an item is added a new heap cell will have to be found and allocated, and potentially the entire contents of the existing array copied to its new location.
But that does not imply a granulatory if 500 should necessarily be chosen. You need to choose the figure with care given the anticipated likley number of elements.
Using a granularity of 1 with CBufFlat/CBufSeg is much worse a crime that should result in imprisonment for 6 months for the offender.
Forum posts: 23
Wohoo , I am so reckless of making a clerical error about the usage of "NewL and NewLC" , as Numpty Alert says ,this
Thank you Andreas !
low-grade fault should result in imprisonment for 6 months for the offender ,
Just Now i turn over the SDK and learn more about the granularity . Actually we need to think about this parameter twice before we use .
Frequently swtich between the User-State and Core-State when re-allocation needs costs the time !!
Thanks Numpty Alert ,Thanks All
Forum posts: 1233
Nope.
I'm pretty sure what takes time is walking the heap to find a new cell, and copy the old data to the new cell, not any context switching to kernel space (if it is at all needed)
btw, didn't see anyone else answer that particular question; Isn't the difference between CleanupResetAndDestroyPushL and CleanupClosePushL quite obvious?
One will call "Close", and the other will call "ResetAndDestroy" on the array.
The difference is documented in the SDK documentation; Close will just deallocate the array, while ResetAndDestroy() also will call "delete" on any pointer stored in the array, so the destructor of the object(s) is called.
One is to be used if your array takes ownership of the object you store pointers too, and the other when it isn't.
Afaik there is no difference between "Close" and "Reset" though.
Forum posts: 672
After calling Close, array space is freed, and you should not use the array anymore since it is "closed" and resources related to using it are freed. After calling Reset, the array space is freed but you still can use the array. That's the basic idea. Have never tried, however, if it hurts to Close the array and still trying to use it...
Forum posts: 23
There is one clear point : both of them ensure that allocated memory is freed, there is no memory leak at all after that .
Forum posts: 672
As long as you remember that Reset and Close (as told above) release the memory allocated to the array (the memory allocated to hold the pointers), but it does not release the memory of the objects pointed to by the pointers in the array. For that, you need to call ResetAndDestroy (as told already). Just wanted to make sure this is understood.
Forum posts: 23
Of course, Andreas !
After online discussion and carefully refining , i have well learned how to use the class " RArray/PointerArray" correctly .