-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Need public API to free returned strings (e.g. cJSON_Print) #5
Comments
You should implement cJSON_Hooks and discourage them from doing the same. |
I propose adding cJSON_Hooks as optional parameter to all the functions that are doing frees/allocations. That would be part of the breaking changes for 1.0.0 |
What is passed will be a pointer to |
Nice. |
While you're there, it might be wise to remove unbuffered printing entirely, especially since you have ensure() working nicely now :) |
I would suggest rename cJSON_Delete to cJSON_DeleteObject and create a new API cJSON_DeleteBuffer which frees the print buffer using hook. Let me know I can create patch for that. |
No. By allowing the user to pass in a pointer to an instance of |
Is there currently any way using cJSON_Hooks to pass along another void pointer that could go to the allocator? I can't see a way. This would be necessary to do something like using a memory pool allocator? Obviously this would require major upheaval to the api, but seems if this is going to happen, now would be the time. Is there any interest in looking at that now, or would it be too invasive to make sense? |
This sounds feasible for v2. My idea was to have an extended Allocator functions that take a void pointer could be added as separate fields to this struct (or be the default, not sure). I will definitely put this feature on the list. |
Yes.. this approach makes sense... the other way to do it.. which probably has value, is to include a concept of a "parent" object, and work the api to pull that void pointer from the parent object. Having access to the parent to be able to walk back up to the root json may also have other unexpected advantages. Anyway all food for thought. We are not using anything like this now, but have at least talked about how it would be useful. |
Interesting idea. But I don't think this works for allocators because any object can be detached from the tree and become it's own root at any time. |
I added This is still quite prone to race conditions though. |
|
There should be a public API to free returned strings like from cJSON_Print().
My usage case is that I'm writing a middleware library that uses cJSON. The user is free to use cJSON elsewhere in their project. I don't necessarily know what memory allocator they are using, so when I call cJSON_Print(), I don't know what free() function to call. cJSON_free is private/internal.
I'm thinking either adding a cJSON_FreeString() which wraps cJSON_free, or making CJSON_free public are the easiest solutions.
Thank you
The text was updated successfully, but these errors were encountered: