Re: [gnome-love] How to know when to free memory.



Good question.  Well, there is basically two answers to your question here:

1) The answer to the specific question about sheet_get_extent() is no.

2) Of coure, you want to know why. :) Well, in this particular case sheet_get_extent returns a struct (Range) not a pointer. (So for one, you'd want: Range range = sheet_get_extent;) You never free() anything that is not a pointer. Why?

Well, the computer divides memory in to a stack and a heap. Variables that you alloc/free are located in the heap. Variables that are just defined (eg, an int) are located on the stack. Variables that are located on the stack have a lifespan limited to the duration of the function. Once a function returns, that memory is no longer valid, and so it doesn't need freeing. However, variables that are *allocated* on the heap will have the lifespan of the program (when the OS will reclaim the memory) or until you tell the OS that it can take that memory back.

So general rule of thumb: if you don't see a * in the type declaration, you will never free it.

The more interesting case is when it DOES return a pointer. Then, well, whether you free it or not is entirely based on the function. In most cases you will probably be required to free memory that is returned to you. The exceptions are important though: 1) If a pointer is returned in to a block of memory (eg from a strchr()) that you already own, don't free it. You'll want to free the block of memory directly when you're done with the whole thing. 2) We have the source. Use it. If a function that returns something keeps a reference to it beyond the lifespan of the function (eg. in a global variable or in a structure somewhere) then you will probably not want to free it, or at least, not right away.


Thats about all I can think of right now. Feel free to ask more questions. Pointers and memory management is an important and powerful tool in C/C++ but it can also be hard to understand. :)

Regards,
Marcus Brubaker

Adrian Custer wrote:

Hey all,

There is some crucial piece of info that I don't get about the gtk
object system. I'm working on a plugin to gnumeric and am trying to
figure out when the return value from some call into the libraries needs
to be freed. As the maintainer Jody says: "you need to understand the
management semantics of all results and arguments." So here's an example
with questions throughout.



GList *sheets;
Range *range;

The range is defined at: http://cvs.gnome.org/lxr/source/gnumeric/src/gnumeric.h#99

Consider this call:
sheets = workbook_sheets (wb);
http://cvs.gnome.org/lxr/source/gnumeric/src/workbook.c#915 which needs to be freed with: g_list_free (sheets);
Similarly consider:
range = sheet_get_extent (aSheet, FALSE);
http://cvs.gnome.org/lxr/source/gnumeric/src/sheet.c#1200
does this need to be freed, and more importantly why/why not? How do I
figure this out?

Thanks for any pointers,
adrian
acuster nature berkeley educational







_______________________________________________
gnome-love mailing list
gnome-love gnome org
http://mail.gnome.org/mailman/listinfo/gnome-love







[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]