开发者

When to rely on garbage collection and when not?

开发者 https://www.devze.com 2023-01-19 04:36 出处:网络
If I\'m going to do this kinda开发者_JS百科 operation many times: res = mysql_perform_query(conn, \"show tables\");

If I'm going to do this kinda开发者_JS百科 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.


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消