Richard , Thanks this works and it is one of the approach that I have
suggested in my last mail ... but the thing is I don't understand that :
1. when we initialize the phandle in myTest() , phtxt(hextxt)="" is pointing
to 0x25ec.
2. when we resize phandle in hexdump(), hextxt(phtxt)="12 34" is pointing to
0x2e00.
3. When we access phtxt(hextxt) after calling hexdump() in myTest(),
phtxt(hextxt)="" is pointing to 0x25ec (same as 1) which is wrong as it is
supposed to point to 0x2e00. This result in NULL value in phtxt and thus, it
gives the error : read directly from unallocatable chunk of memory...
This shows that the Char* phtxt has been passed by value to hexdump() ????
This is odd and I couldn't figure out the reason .... In fact, I 've solved
it as mentioned in my last mail using 2 approaches but I just wanna know why
does this happen ....
Please comment... thanks
eg :
myTest()
{
....
phandle = MemHandleNew(1);
phtxt = (Char*) MemHandleLock(phandle); --> phtxt is allocated at 0x25ec
....
hexdump(ptxt,phtxt,phandle);
.... -->after hexdump, phtxt is allocated at 0x25ec
}
hexdump(Char* stxt, Char* hextxt, MemHandle phandle)
{
...........
MemHandleUnlock(phandle);
MemHandleResize(phandle, 3*StrLen(slen+1));
hextxt = (Char*) MemHandleLock(phandle); -->phtxt is allocated at 0x2e00
StrCopy(hextxt, htxt);
........
}
Regards,
Tan Kuan Eeik
[EMAIL PROTECTED]
> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED]]On Behalf
> Of Richard
> Burmeister
> Sent: Tuesday, July 03, 2001 11:43 PM
> To: Palm Developer Forum
> Subject: RE: String error : just read directly from
> unallocated chunk of
> memory.
>
>
> > From: Tan Kuan Eeik
> >
> > I 've mark the link where error occurs and here 's the
> error and the code.
> > This is caused by trying to access the phtxt ...
> >
>
> Your code (snipped) has so much extra stuff going on (for
> example, you pass
> in a handle and a pointer to the same block of memory, then unlock and
> resize the handle, then lock it again and assign it to the
> original pointer)
> that I just rewrote it entirely. Here is my code. (I'm sure someone
> reading this would write the code differently, but this
> closely mirrors your
> original code.)
>
> // My version of hexdump, without hexHandle or slen params.
> // Creates a hex version of stxt, and returns it in hextxt.
> static void hexdump(Char* stxt, Char* hextxt)
> {
> MemHandle hhnd;
> Char* htxt;
> Int16 i, slen;
> Char hs[] = "0123456789ABCDEF";
>
> if (!stxt || StrLen(stxt) == 0)
> StrCopy(hextxt, "");
> else {
> slen = StrLen(stxt);
> hhnd = MemHandleNew(slen ? 3*(slen+1) : 1);
> htxt = (Char *)MemHandleLock(hhnd);
> for(i=0;i<slen;++i) {
> htxt[3*i] = hs[((unsigned char)stxt[i])>>4];
> htxt[3*i+1] = hs[((unsigned char)stxt[i])&15];
> htxt[3*i+2] = ' ';
> }
> if (slen)
> htxt[3*i-1] = '\0';
> else
> htxt[0] = '\0';
> StrCopy(hextxt, htxt);
> MemHandleUnlock(hhnd);
> MemHandleFree(hhnd);
> }
> }
>
> // code to call hexdump
> static void myTest(void)
> {
> Char *phtxt, *ptxt;
> UInt16 plen;
> MemHandle phandle;
>
> ptxt = "123456789";
>
> // get space for the hex string
> plen = StrLen(ptxt);
> phandle = MemHandleNew(plen*3+1);
> phtxt = (Char*) MemHandleLock(phandle);
>
> hexdump(ptxt, phtxt);
> FrmCustomAlert(MyAlertAlert, "crypto() - 5", ptxt, " ");
> FrmCustomAlert(MyAlertAlert,"crypto() - 10", " ", phtxt);
> MemHandleUnlock(phandle);
> MemHandleFree(phandle);
> }
>
>
>
> --
> For information on using the Palm Developer Forums, or to
> unsubscribe, please see http://www.palmos.com/dev/tech/support/forums/
>
>
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/tech/support/forums/