Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak in callbackRetText function #1259

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

hionay
Copy link

@hionay hionay commented Jul 12, 2024

This PR fixes the memory leak in the callbackRetText function by freeing the memory allocated by the code:

C.CString(v.Interface().(string))

Copy link

@Imran-imtiaz48 Imran-imtiaz48 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

  1. Safety and Memory Management:

    • The latest version introduces proper memory management by using defer C.free(unsafe.Pointer(cstr)) to ensure that the allocated C string is freed, preventing memory leaks. This is a significant improvement over the previous version, which did not free the allocated memory.
  2. Readability and Maintainability:

    • Both versions are fairly readable and maintainable. However, the latest version's use of defer for freeing memory makes the intent clear and the code more idiomatic Go.
  3. Error Handling:

    • The error handling for the type check is appropriate in both versions. However, you might want to include the actual type in the error message for better debugging.

Suggestions for Improvement

  1. Enhanced Error Message:

    • Improve the error message to include the actual type of v. This will make debugging easier.
    • Example: return fmt.Errorf("cannot convert %s to TEXT, expected string", v.Type())
  2. Use of unsafe.Pointer:

    • While the use of unsafe.Pointer is necessary here, always ensure that this is well-documented, as it can be error-prone if misused.

Revised Code with Suggested Improvements

if v.Type().Kind() != reflect.String {
	return fmt.Errorf("cannot convert %s to TEXT, expected string", v.Type())
}
cstr := C.CString(v.Interface().(string))
defer C.free(unsafe.Pointer(cstr))
C._sqlite3_result_text(ctx, cstr)
return nil

Conclusion

The latest version of the code is a good improvement over the previous version, particularly in terms of memory management. The suggested enhancement for the error message will further improve the code's clarity and debuggability.

@otoolep
Copy link
Contributor

otoolep commented Jul 13, 2024

Real LLM vibes with that last review comment.

@hionay
Copy link
Author

hionay commented Jul 14, 2024

Hi @mattn, how can I help in getting this merged?

@charlievieth
Copy link
Contributor

@mattn / @rittneje this is a pretty straightforward (and correct) fix for an obvious bug - could either of you take a look at this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants