industrialNETworXnetx

Krasi Gichev

Krasi Gichev

AMK

| 09.10.2009 | 13:47 | 2 replies

rX_MemAllocateMemory allocates in already used memory?

Hi,
we are performing a simple test:
- in a dynamically created task we try to allocate large chunk of memory - 40000 bytes. rX_MemAllocateMemory returns RX_OK but the pointer that it returns points to already used memory. If we perform memset on the allocated area, our local variable in the task get erased (overwritten) by memset ???

Here is the code:

void  OurTest_Task (void *p_arg)
{
     
    rX_SysSetTaskInitialized(NULL); // notify rxC that our init is done
   
    void* pStack;
    int size = 40000;  // 40000 = 0x9C40
    if (RX_OK == rX_MemAllocateMemory(&pStack, size)) 
    {
           // at this line, we have valid pointer 0x8005A710 and size=40000
       rX_memset(pStack, 0, size);
          // when memset returns, the pStack and size are erased - both are 0 ????
    }

while(1)
{
rX_SysSleepTask(1500);
}

}

Allocated memory is at address 0x8005A710, the end of the allocated area should be 0x8005A710 + 0x9C40 = 0x80064350 :
But the local variable "size" is located at 0x80061E54, the pStack pointer is at 0x80061E50.

So it is obvious why the local variables are erased.

Probably we are missing something, I am not aware how the memory allocation in rcX works?

The task stack is small - 10240 bytes.
But the static memory pool is large enough:
#define RX_STATIC_MEMORY_SIZE 1048576

Here is how this task is created: (IS THIS CORRECT - to use dynamically allocated memory as task stack ???)

  if (RX_OK == rX_MemAllocateMemory(&pTaskStack, 10240))
  {
    rX_SysCreateTask(   "OurTestTask", 
                        OurTest_Task,  
                        (void*)NULL,  
                        (void*)pTaskStack, 
                        10240,
                        RX_TASK_AUTO_START, 
                        0, 
                        TSK_PRIO_51, 
                        TSK_TOK_51, 
                        0, 
                        (void (*) (void*))NULL); 
  }

Task stack is at 0x80057E68, so with task stack size of 10240, the task stack is from 0x80057E68 to 0x8005A668.

Local variables in the task are located at:
size @ 0x80061E54
pStack @ 0x80061E50

Allocated chuck of data is at:
pStack = 0x8005A710 (the 40000 bytes are from 0x8005A710 .. 0x80064350)

So, this allocated memory is not in the task stack area (I guess this is the idea in the rcX) - but why it overlaps with the local variables (stack?)?

PS: Here http://board.hilscher.com/viewtopic.php?f=1&t=371 I saw that somebody creates dynamically a task without allocated stack ( 0 ) - is this correct and when should I specify a stack and when not?

M T

M T

Hilscher Gesellschaft für Systemautomation mbH

| 13.10.2009 | 09:04

At which time, are you creating the task? From function main(), Callback from Kernel, or in another static task?

The memory allocation is initialized during startup of the kernel, and if you try to create a task before kernel runs, the memory allocator gets reset and you may get the same pointer again (the old one is no longer valid).

BTW:

krassimir wrote:

#define RX_STATIC_MEMORY_SIZE    1048576


This define can safely be removed, as it is referenced nowhere. Memory allocator initialization is done in the linker file (see heap_start, _heap_end).

Regards

MT

Krasi Gichev

Krasi Gichev

AMK

| 13.10.2009 | 10:01

Hi,
thank you for your input.

We have found the problem:

The argument of SysCreateTask() for stack size is defined as "stack elements"; each stack element is 4 bytes.

Because the tasks are created dynamically, I call the AllocateMemory with size equal to the stack size - but in bytes; And the allocated area is 4-times smaller that the one assumed by the CreateTask.

I had to change the Allocate size to "stack_size" * 4 before calling CreateTask with "stack_size" size and pointer to the
allocated area.

Best regards.

Login