views:

98

answers:

2

If I'm going to do this kinda operation many times:

  res = mysql_perform_query(conn, "show tables");

  printf("MySQL Tables in mysql database:\n");
  while ((row = mysql_fetch_row(res)) !=NULL)
      printf("%s\n", row[0]);

Do I need to run mysql_free_result(res); at the end of each operation or rely on garbage collection mechanism , why?

UPDATE

I still don't see clearly how to judge whether some data structure needs to clean up according to existing answer.

+2  A: 

In C++, never. C++ is not garbage-collected. The closest would be RAII, which is basically wrapping stuff that needs cleanup in objects that do said cleanup in their destructor. I can't give further detail since I don't really use C++ all that much.

As an aside: Even in a GC'd language, GC only works well for memory. Explicit cleanup's still needed for stuff like SQL connections, file handles, etc.

Bottom Line: You always have to clean up once you're done. (Unless the documentation says it's not needed)

Kyte
Why don't I need to clean up `row` each time then?
ollydbg
Sorry, bad phrasing on my part. I answered the title, not the question body. I meant "never rely on GC because it's not there". Either clean up every time or cache and reuse the row (if applicable).
Kyte
@ollydbg - You should clean up row each time as well but that depends on your implementation, as to in which scope you are declaring row, and if you are reusing the same or not.
Als
But there is no such an API to clean up `MYSQL_ROW`
ollydbg
@Als,isn't the `row` in my example different on each loop obviously?
ollydbg
Well, if there's no API to clean up the row, then it's safe to assume you don't need to clean it up, don't you think? Check the docs, they should say if something needs cleanup.
Kyte
Can't we infer whether something needs to clean up by the code itself?
ollydbg
Just looking at the code will never give you the whole picture. You might miss crucial details.
Kyte
*MYSQL_ROW row;* is a variable that is not allocated by malloc(). When the execution flow leaves main(), this resource is going to be destroyed. Each time your loop iterates "row" is overwritten with a new value and no new memory is allocated for it.
karlphillip
@ollydbg - sorry I am unfamiliar with PHP API's. looks like the API returns you a array "row" and then you can use that array further on.Also, it is not safe to assume that cleanups will be done by an API. The API doccumentation will clearly mention whether it will clear the dynamic allocations or you need to do so.
Als
The essence of my question is to infer whether to clean up by the function declaration,not according to the doc.
ollydbg
Then the answer will always be no, you can't infer such a thing.
Kyte
A: 

To clarify things: Dynamic memory allocations are made on the Heap and should be deallocated explicitly(allocated by new/malloc deallocated by delete/free)
so the basic rule is:
Every dynamically allocated variable allocated heap memory using free or new will have to be deallocated explicitly by calling free and delete respectively.(exception: Smart pointers)

When to rely on garbage collection and when not?
Simple Answer is in case of C++ NEVER. C++ does not have a garbage collector as Java.

How to judge whether some data structure needs to clean up?
1. Check If the structure is dynamically allocated. If YES then it is candidate for clean up.
2. Read the API doccumentation of API which returns or creates the data structure, to see if the API expects you to de-allocate memory of the data structure.
3. To overcome this problem of explicitly deallocating memory, C++ has special types of implementations called "Smart pointers" using the principle of "RAII". If your data struture is a smart pointer you need not bother about the clean up. Read more about the smart pointers, lots of threads in here too and I am sure it will help you understand answer to your question.

Als
Here's the definition :`typedef char **MYSQL_ROW;` So obviously `MYSQL_ROW` is also created from memory allocation, why just we don't need to deallocate it? Or does it mean c++ can deal with deallocation of `char **` automatically?
ollydbg
Als
Oops my bad, please ignore previous, row is a 2 dimiensonal array of characters, not dynamically allocated!
Als
Do you mean row is of fixed size?
ollydbg
@ollydbg Yes! it is an array on stack.
Als