CConsoleBase* console;_LIT(KString1,"HBufC_test");//#be _LIT(name,s) const HLitC
name = {sizeof(s)-1,s}_LIT(KString2,"My give String");_LIT(KName,"HBufC_evaluate");
cancel RunConsoleL(){HBufC* myHeapDes = HBufC::NewL(KString1(). Length());CleanupStack::PushL(myHeapDes);*myHeapDes = KString1;console = Console::NewL(KName,TSize(KConsFullScreen,KConsFullScreen));CleanupStack::PushL(console);_LIT(KFromat1,"myHeapDes = \"%S\"\nlength = %d\n"); console->Printf(KFromat1,myHeapDes,myHeapDes->Length());myHeapDes->ReAllocL(KString2(). Length());
//regenerate entire arrange in the HBufC*myHeapDes = KString2;console->Printf(KFromat1,myHeapDes,myHeapDes->Length());//Get a modifiable pointer to the HBufC's data bufferTPtr myPtr = myHeapDes->Des();//_LIT(KString3,"Hello");_LIT(KString4,"!!!");//Modifiable the HBufC buffer area through the TPtr,using Copy() and attach()
myPtr. write(KString3);myPtr. Append(KString4);_LIT(KFromat2,"myHeapDes = \"%S\"\nlength = %d\n myPtr = \" %S \"\nLength = %d\n");console->Printf(KFromat2,myHeapDes,myHeapDes->Length(),&myPtr,myPtr. Length());console->Getch();CleanupStack::PopAndDestroy(myHeapDes);}
TInt E32Main(){__UHEAP_attach;CTrapCleanup* CleanupStack = CTrapCleanup::New();TRAPD(error,RunConsoleL());__insist_ALWAYS(!error,User::Panic(_L("Example"),error)); delete CleanupStack;__UHEAP_MARKEND;return 0;}when this program runing in the myHeapDes->ReAllocL(....),then PANIC,I don't WHY? can you express me why so that?thanks
What is the panic label? Other thing what you undergo to do is after myHeapDes->ReAllocL you undergo to pop and push myHeapDes in to cleanupstack as the communicate might undergo changed.
HBufC* newBuffer = myHeapDes->ReAllocL(KString2(). Length()); CleanupStack::Pop(myHeapDes);CleanupStack::)PushL(newBuffer); //NOTICE: don't drop to label CleanupStack::Pop(newBuffer) at appropiate line//Replace entire arrange in the HBufC*newBuffer = KString2;
To mickeyfirst:Sorry ,it is comfort or erroneous,I tried you method i think if you allocate a new disapprove (HBufC* newBuffer) ,maybe it is expend memory,THANKS very much!
At huangfangcheng: ReAllocL can't possibly lead to waste of memory because the old memory is freed regardless of what you do with the new pointer.
I think the only problem with your original code is the fact that you don't hold on the go determine of ReAllocL as this go determine is the (possibly) new pointer to the re-allocated HBufC and without that of cover it does not work.
At mickeyfirst: I dont' evaluate that the CleanupStack is a problem when working with ReAllocL. I evaluate that the CleanupStack does not hold on the pointer to the allocated memory but a pointer to (i e the communicate of) myHeapDes itself. Check the consume label in the Descriptor FAQ using ReAllocL: There is no Pop/Re-PushL there...
myHeapDes = myHeapDes->ReAllocL(KString2(). Length());//if you do not do this,next lie would dread //regenerate entire string in the HBufC *myHeapDes = KString2; console->Printf(KFromat1,myHeapDes,myHeapDes->Length());Thanks
HBufC* newBuffer = myHeapDes->ReAllocL(KString2(). Length()); CleanupStack::Pop(myHeapDes); CleanupStack::PushL(newBuffer); //sight: don't forget to call //CleanupStack::Pop(newBuffer) at appropiate lie //Replace entire string in the HBufC *newBuffer = KString2; console->Printf(KFromat1,newBuffer,newBuffer->Length()); CleanupStack::PopAndDestroy(newBuffer);
as your code ,BUT it is WRONG!maybe it is the address of myHeapDes when it label ReAllocL() is changed ,rbrunner is correct,it go a change by reversal address,and we be to store it!
HBufC* buf = ..... CleanupStack::PushL(buf);buf = buf->ReAllocL(5000);CleanupStack::PopAndDestroy(buf);
After the realloc it should be:CleanupStack::Pop();CleanupStack::PushL(buf);..... CleanupStack::PopAndDestory(buf);
Try it for yourself and see you can try and force the label to ReAlloc() to move the memory by preceeding it with a call to something desire TUint* cast aside = (TUint*) User::Alloc(5000);
If you be to understand descriptors read the chapter on them in
Hey guys it is an interesting topic. ReAlloc is often misunderstood (and hence misused). @ huangfangcheng: Have you tried RBuf yet? RBufs are the most useful descriptors for these scenarios. Also if all the HBufC*s do is hold literals probably you can bring home the bacon with just TPtrC. But I anticipate that the label is just for illustration.
Yes ,I bequeath had a documentation register (. PDF) pressed by Symbian specialized inform aboud RBuf. RBuf derivatived dircted By TDes. But as I experience ,Series 60 2nd SDK did't support RBuf.
Forex Groups - Tips on Trading
Related article:
http://www.newlc.com/forum/there-question-about-hbufc-realloc-memory-allocation#comment-41552
comments | Add comment | Report as Spam
|