tags:

views:

141

answers:

3

i am passing HBuf to function but it crashes i don't know whether i am following right way or not

//case 1:

HBufC8* iBuffer2 = HBufC8::NewL(1000 );

TPtr8 bufferPtr( iBuffer2->Des() );

 //assigning value to HBuf code


StartParsingL(iBuffer2);

StartParsingL(HBufC8* aHBufPtr)
{

  iBuffer = HBufC8::NewL(aHBufPtr->Length());//it crashes here
  bufferPtr.Copy(aHBufPtr->Des());//also here
}
A: 

In the Symbian coding convention, "i" prefixed variables are member variables.

In your snippet you have it as a local declaration. It could be that you just put in the type for clarity of the snippet, but if you've declared it both as a member variable and as a local declaration, that could explain plenty of crashes :-)

You should have:

class C....: public CBase?....
   {
private:
   HBufC8* iBuffer2;
   };

...

void C...::ConstructL()
   {
   ...
   iBuffer2 = HBufC8::NewL(1000);
   ...
   }
Will
A: 

This code does not directly show the reason for crashing. Please post actual snippets next time.

Also, when posting about a crash, it is good to know how it is crashing. I assume it is a KERN-EXEC 3 panic. But please be explicit about it yourself.

I guess the aHBufPtr passed to StartParsingL is either zero or otherwise does not point to a valid HBuf8 object. That would be the immediate reason for the crash. Why it is not a valid pointer is not visible in the code, but one reason could be shadowing a member variable with a local variable as suspected by @Will.

Some further points, not related to crash here but Symbian C++ in general:

  • When passing descriptors around, you should use the most general read-only TDesC or TDesC8 type references or read-write TDes or TDes8 type references. Use HBufC or HBufC8 pointers only if you are transferring ownership to the callee.

  • In this snippet: bufferPtr.Copy(aHBufPtr->Des()); Copy accepts a TDesC& so you do not need to call Des: bufferPtr.Copy(*aHBufPtr); Though if you just want to copy a descriptor, you could use any of the descriptor Alloc functions such as AllocL.

laalto
+1  A: 

Not answering your question (as there isn't really enough information). I would recommend using an RBuf instead of a HBuf as they are the recommended heap buffer descriptor going forward, and a bit easier to use.

steprobe
Yes I would recommend RBuf to anybody new to Symbian. Having to mess around with the different type of descriptor and working out how to convert between them really isn't a useful introduction to Symbian.
frankster