Patch: support callback on allocation overflow instead of calling abort()

Marc Glisse marc.glisse at
Tue Oct 6 11:45:04 UTC 2020

On Tue, 6 Oct 2020, Vincent Lefevre wrote:

> On 2020-10-06 11:51:29 +0200, Marc Glisse wrote:
>> On Tue, 6 Oct 2020, Vincent Lefevre wrote:
>>> On 2020-10-06 04:41:42 +0100, Colin Caine wrote:
>>>> This is used by Julia to raise an OutOfMemory exception rather than having
>>>> the Julia process itself abort.
>>>> Others on stackoverflow, etc, have experienced similar problems with
>>>> undesired aborts(), so this patch would probably be useful to a variety of
>>>> users. (see:
>>>> More information is available here and in the linked issues and PRs:
>>> Since the function could have allocated memory for intermediate
>>> computations, how do you avoid memory leaks?
>> You would need something like
> But this is only for C++

Yes, that's for people willing to compile GMP with a C++ compiler. I don't 
think that's such a big issue, except on windows.

> and only for temporary memory (while normal allocation seems to be 
> sometimes used, like in mpz/mul.c with "wp = __GMP_ALLOCATE_FUNC_LIMBS 
> (wsize);").

With a single allocation, quite often you won't leak, since either it 
succeeds and you are fine, or it fails and you don't have any new memory 
to leak. But yes, making it work 100% would require auditing a lot of 

Marc Glisse

More information about the gmp-bugs mailing list