Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7716903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:44:57+00:00 2026-06-01T02:44:57+00:00

This is a really novice question, but I don’t write C unless (err, if)

  • 0

This is a really novice question, but I don’t write C unless (err, if) I can avoid it 😉

I’ve written a small extension to ruby that needed to interface with libmysql. It works as expected, but now I’m in some doubt as to whether two lines of code a resulting in a possible memory leak.

Inside a tight loop I’m using functions MYSQL_FIELD * mysql_fetch_fields( ... ) and unsigned long * mysql_fetch_lengths( ... ).

Since these functions return arrays, I assume they would use malloc() therefore require the user to call free() manually when finished with the result? I expected this to be documented in the manual, but it’s not, so I assume this is one of those things C developers just do instinctively: http://dev.mysql.com/doc/refman/5.0/en/mysql-fetch-fields.html

Any pointers? (in the advice sense ;))

The code in question is here: https://github.com/d11wtq/oedipus/blob/master/ext/oedipus/oedipus.c#L137-138

EDIT | Ugh, now I have a doubt. It is documented to call mysql_free_result() on the result set, which was passed to mysql_fetch_fields(), so perhaps this is just returning a pointer to something that’s already on the heap, from that MYSQL_RES struct.

EDIT 2 | Sorry for the noise. It does appear that this information is just pulled from the MYSQL_RES struct which is an argument to mysql_fetch_fields(), and is itself freed later, so I’m probably fine:

typedef struct st_mysql_res {
  my_ulonglong  row_count;
  MYSQL_FIELD   *fields;
  MYSQL_DATA    *data;
  MYSQL_ROWS    *data_cursor;
  unsigned long *lengths;               /* column lengths of current row */
  MYSQL         *handle;                /* for unbuffered reads */
  const struct st_mysql_methods *methods;
  MYSQL_ROW     row;                    /* If unbuffered read */
  MYSQL_ROW     current_row;            /* buffer to current row */
  MEM_ROOT      field_alloc;
  unsigned int  field_count, current_field;
  my_bool       eof;                    /* Used by mysql_fetch_row */
  /* mysql_stmt_close() had to cancel this result */
  my_bool       unbuffered_fetch_cancelled;  
  void *extension;
} MYSQL_RES;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:44:58+00:00Added an answer on June 1, 2026 at 2:44 am

    You need to call mysql_free_result(result) where result is the result of mysql_fetch_fields. C developers are used to this programming paradigm, but there is no one function you call to free something allocated in another library. Each library will have functions that allocate and return something, then a function to free the results of the allocation. The client can’t free it itself with free or delete as it may be allocated on a different heap, and may be a complex object with multiple allocations inside.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I notice this question has been asked a few times but I don't really
Right this is a really novice question i think.. but I've not got any
This really seems like a bug to me, but perhaps some databinding gurus can
This seems like a really amateur question, in my opinion, but nonetheless it's still
This is almost certainly a very novice question, but being as I am a
I'm it really sure how to explain this question but i'll give it a
This really, really urks me, so I hope that someone can give me a
This really has my stumped today. I'm sure its simple, but... Here is my
This really shouldn't be hard, I just can't figure out how to do it.
This really puzzled for hours, I searched all over the internet, but got no

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.