[Date Prev][Date Next] [Thread Prev][Thread Next]
[Date Index] [Thread Index] [New search]

Re: FDK:Question about F_Free



On Thu, 16 Jan 2003 11:09:21 -0500, "Rick Quatro" <rick@frameexpert.com> wrote:

>I have an intermittent crash with one of my plugins. It appears to be
>happening when I free a string with F_Free. For example, I have this:
>
>CharT vBuf[30];
>....
> // Convert the absolute page number to a string.
> F_Sprintf (vBuf, "%d", vAbsolutePage);
> s = F_StrCopyString(vBuf);
> F_Free(vBuf);
>
>If I comment out F_Free(vBuf) I don't get the crash.

Right.  Never free something that you didn't allocate on the heap with
F_Alloc() or one of the other F_ allocation functions like F_StrNew()!
Or that the FDK didn't allocate that way for you as in F_StrCopyString().
So in the case above, you can F_Free(s), but not vBuf.

>My question is, if I use F_Free(vBuf), can I later use vBuf in the same
>subroutine? Also, what is the danger if I don't use F_Free at all to free
>the string? Thanks in advance.

If you had used:
 StringT vBuf = F_Alloc(30);
then you could F_Free, then F_Alloc again and re-use all you want.
Once you have used F_Free(ptr), though, you must *not* dereference
ptr again until you have assigned some valid address to it, as you
did with s above, or used F_Alloc to assign storage to it again.

You might want to read some C-language docs on memory allocation,
specifically malloc() and free(), to get a better notion of what
is going on here.  The FDK just provides a simple wrapper of the
C library functions (which you can also use directly, BTW).

HTH!

-- Jeremy H. Griffith, at Omni Systems Inc.
  (jeremy@omsys.com)  http://www.omsys.com/

** To unsubscribe, send a message to majordomo@omsys.com **
** with "unsubscribe framers" (no quotes) in the body.   **