Skip to content

Context Reallocate fails to allocate memory #7

@jarutis

Description

@jarutis

Hi,

I wanted to write an UDA which accumulates values and does some calculation in the final step. This works fine for very small samples. But `context->Reallocate' fails at arround 4MB. It returns a valid pointer but with allocated only 56 bytes.

Here is my Update code which accumulates values.

void DistributionUpdate(FunctionContext* context, const DoubleVal& input,
                        const IntVal& bins, StringVal* val) {
  if (input.is_null) return;
  DistributionStruct* dist = reinterpret_cast<DistributionStruct*>(val->ptr);

  if(dist->count >= dist->capacity) {
    dist->capacity += ELEMENT_ALLOCATION_SIZE;
    int new_size = val->len + dist->capacity * sizeof(double);
    val->ptr = context->Reallocate(val->ptr, new_size);
  }
  dist->elements[dist->count++] = input.val;
  dist->bins = bins.val;
}

If I debug this and check what is allocated versus what I expect to get allocated I get:

--TESTING NULL--
--TESTING 10 VALUE SAMPLE--
--TESTING NULL VALUE SAMPLE--
--TESTING COUNT < BINS--
--TESTING COUNT == BINS--
--TESTING 1000 UNSORTED VALUE SAMPLE--
--TESTING 1.000.000 VALUE SAMPLE--
[Reallocating old size:] Message: 800040
[Reallocating expected new size:] Message: 1600032
[Reallocating allocated new size:] Message: 1600040
[Reallocating old size:] Message: 1600040
[Reallocating expected new size:] Message: 2400032
[Reallocating allocated new size:] Message: 2400040
[Reallocating old size:] Message: 2400040
[Reallocating expected new size:] Message: 3200032
[Reallocating allocated new size:] Message: 3200040
[Reallocating old size:] Message: 3200040
[Reallocating expected new size:] Message: 4000032
[Reallocating allocated new size:] Message: 4000040
[Reallocating old size:] Message: 4000040
[Reallocating expected new size:] Message: 4800032
[Reallocating allocated new size:] Message: 56
Segmentation fault

Am I hitting some kind of limits, what am I missing here?

My full code is here:
https://gist.github.com/jarutis/7bb4e93a137607607a42

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions