> -----Original Message-----
> From: Dave Lippincott [mailto:[EMAIL PROTECTED]]
>
> Hi
> I'm having a problem passing a pointer between functions. I
> have a function
> that takes a Handle and CharPtr. The function locks the
> handle and returns
> the pointer to the string but the CharPtr always returns as NULL (even
> though the chunk was locked properly). Is this a problem
> with CodeWarrior
> (R5) or am I doing this the wrong way?
>
> Thanks
> Dave
>
> <sample code>
> CharPtr Buffer;
> Handle MemHandle;
> .
> .
> .
> MemHandle = MemHandleNew(size+1);
> FunctionX(MemHandle,Buffer);
> // At this point Buffer is NULL - Why?
Because you haven't done anything with it?
>
> UInt FunctionX(Handle Source,CharPtr Result)
> {
> // do stuff that doesn't impact result or source
>
> Result = MemHandleLock(Source);
> // Result is the correct value here
>
> return retval;
> }
Oh, I see. You want have FunctionX() lock the
handle and have the resulting pointer stuffed
into the variable "buffer" in the calling routine.
The best way would be to not use args, but have
FunctionX() -return- the locked pointer. But if your
UInt return is some sort of result code, that option
may not be available to you.
But barring that, what you really need is a
pointer to your pointer.
Handle MemHandle;
CharPtr Buffer;
MemHandle = MenHandleNew(size+1);
FunctionX(MemHandle, &Buffer);
...
UInt FunctionX(Handle Source, CharPtr *pBufPtr)
{
(*pBufPtr) = MemHandleLock(Source);
...
Of course, the desirability of having a subroutine
lock the handle and leaving the responsibility of
unlocking it up to the calling routine is questionable.
Generally it makes code safer if you lock & unlock
at the same place. Either both operations should be
done in the subroutine FunctionX(), or both should
be done in the calling routine -- i.e. lock the handle
and then just pass the pointer to FunctionX(), which
would then not need the handle.
Of course, there may be reasons that you need to do
it this way ... in which case all I can say is: be
careful.
--
-Richard M. Hartman
[EMAIL PROTECTED]
186,000 mi./sec ... not just a good idea, it's the LAW!