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

Fixed 'memleak' detected by cppcheck #138

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

Conversation

kostyk348
Copy link

@kostyk348 kostyk348 commented Mar 23, 2023

In terms of curiosity run cppcheck on alpaca.cpp and got that message:

Checking chat.cpp ...
chat.cpp:564:9: error: Common realloc mistake: 'buf' nulled but not freed upon failure [memleakOnRealloc]
buf = realloc(buf, buf_size);
^
Checking chat.cpp: _WIN32...
Checking chat.cpp: _WIN32;APPLE;MACH;unix...
Checking chat.cpp: APPLE;MACH;unix...
Checking chat.cpp: __ARM_NEON...

Nothing serious I guess, but now there's no errors and everything builds ok

@michieal
Copy link

michieal commented Apr 4, 2023

Here's an alternate way to handle it, if you like it better. I saw the memleak in cppcheck too, and I fixed it on my end before coming across this. Seeing it, I thought that I could share my code too, and people would have options. (I don't really think that it matters which one a person goes with, as they both do effectively the same thing.)

	void* old_buffer = buf;

        // reallocate
        buf_size = buf_size_new;
        buf = realloc(buf, buf_size);
        if (buf == nullptr) {
            fprintf(stderr, "%s: failed to allocate %zu bytes\n", __func__, buf_size);
	    free(old_buffer); // prevent memory leakage.
            return false;
        }
    }

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.

None yet

2 participants