-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Leak in cores/esp32/WString.cpp when malloc fails #710
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
Comments
I agree, if the malloc(newsize) fails, the function should return 0 (fail) and NOT NULL the buffer pointer.
But, why is malloc() used instead of realloc()? To me, this is the perfect case to use realloc()? So, something like this:
Chuck. |
also ok .. just free(buffer) before assigning a new pointer to it.. |
If you free the buffer make sure you update the capacity. Also, if you free the buffer how does that effect the next time this library is called? And is the error propagated back to the user app? I would think this failure should generate either an abort or log event. Chuck. |
How about this: unsigned char String::changeBuffer(unsigned int maxStrLen)
{
size_t newSize = ((maxStrLen + 16) & (~0xf)) - 1;
char *newbuffer = (char *) realloc(buffer, newSize+1);
if(newbuffer) {
if(newSize > len){
if(newSize > capacity){
memset(newbuffer+capacity+1, 0, newSize-capacity);
}
} else {
//new buffer can not fit the old len
newbuffer[newSize] = 0;
len = newSize;
}
capacity = newSize;
buffer = newbuffer;
return 1;
}
log_e("realloc failed! Buffer unchanged");
return 0;
} |
@me-no-dev , Yes, Better. Chuck. |
@stickbreaker I have the change staged :) was just waiting for confirmation in case I missed something |
i keep getting this error [E][WString.cpp:185] changeBuffer(): realloc failed! Buffer unchanged |
you are running out of memory |
thx @me-no-dev! the heap shows plenty of free space (checked with esp_get_free_heap_size()) and in the 'main' thread i still have >6000 of the allocated memory (checked with uxTaskGetStackHighWaterMark( NULL )).
|
Stack is not Heap. |
I think on allocation failure nothing should happen and 0 should be returned.
The text was updated successfully, but these errors were encountered: