Where is TBuf8 stored for local variables????I am confused

Login to reply to this topic.
Tue, 2007-12-04 07:07
Joined: 2007-11-02
Forum posts: 37

Hi,
I used to think that the TBuf has the below mentioned locations when declared as :-
a> class variable----- on stack
b> local variable---- on heap

I tried the following code----
class X
{
----------
void fn();

}

X::fn()
{
----------
TBuf8<20> progress;//I felt that this "progress variable"is
//created on heap and needs to be managed
//for memory leakage
CleanupStack::PushL(&progress);
-------------
-------------
CleanupStack::PopAndDestroy();
-----------
-----------
}

This code crashed with "User 42 panic" untill I commented both the push and pop operations.

From documentation I found that some situations for this panic are:------
a>freeing memory which is not null and does not point to valid memory cell
b> in case of freeing "RRegion" objects not on heap but on stack.

MY QUESTIONS
---------------
1.If the TBuf is on heap,how to manage it for memory leakage?
2.If TBuf is on stack, then any one situation where it is on heap(example)
3.If I am not wrong, this code suggests me that we can test any variable is on the stack or heap by following:---
CleanupStack::pushL(object);
CleanupStack::PopAndDestroy(object);
if the variable "object" is on the stack, it will raise panic "user 42",,,,,,,,if on heap then no panic


Tue, 2007-12-04 07:24
Joined: 2005-11-20
Forum posts: 1242
Re: Where is TBuf8 stored for local variables

Maybe the most important question is: Where does your confusion come from?

I would say that memory-wise a TBuf is as harmless as a lowly TInt or TBool: You most probably won't run into any memory leak trouble whatsoever with it.

It is also no question where it is, on the stack or on the heap: It is there where you put it. If you declare one inside a method, it goes onto the stack, but does not need management by cleanup stack. If you declare one as a member variable of an object, you could say "it goes into the heap", but always as part of the object, not on its own, as any TInt would do as well, for example. You will have to care about the *object*, not about any TBuf inside it.

Of course you could allocate a TBuf on the heap yourself, as you could even allocate a TBool on the heap, but I don't know of any situation where this would make sense. But if you were, you would have to use the cleanup stack.


René Brunner

Tue, 2007-12-04 07:51
Joined: 2007-09-20
Forum posts: 114
Re: Where is TBuf8 stored for local variables????I am confused

Hi,

Any automatic variable (of any type) goes on to the stack if they are declared inside a method as local variable.

The T-Classes can be orphaned on the stack becuase T-Classes do not own external resources.

No need to push the T-Class to clean up stack .


Chao,
Raghav

  • Login to reply to this topic.