Forums | developer.brewmp.com Forums | developer.brewmp.com

Developer

Forums

Forums:

Hi Friends,

I am using BREW 3.1.2. The application crashes as MALLOC() fails to allocate memory. Is this due to some kind of memory shortage??

Can anyone please help me to find out on what all grounds MALLOC() could fail to allocate memory?

Thanks in advance...... :)

Hello akshay,
could it be because you're allocating too much in just one batch? try looking at the device's details in the emulator (under memory section). see if you're not allocating too much that the phone might not have. if that is the case (assuming you're talking about emulator only), try using a different device pack with more memory space, and see if it works.
If this solution is not helpful, then there could be the possibility that your FREE(), MALLOC() sequence is bad somewhere in your code, because I believe sometimes malloc just fails, or a line or two after malloc in execution where there is apparently no problem wrt code.
If malloc, free sequence is bad, or you're somewhere else writing to memory more than the amount allocated, then theres problem..
Just few cases I've listed down ... maybe something clicks...

Hello akshay,
could it be because you're allocating too much in just one batch? try looking at the device's details in the emulator (under memory section). see if you're not allocating too much that the phone might not have. if that is the case (assuming you're talking about emulator only), try using a different device pack with more memory space, and see if it works.
If this solution is not helpful, then there could be the possibility that your FREE(), MALLOC() sequence is bad somewhere in your code, because I believe sometimes malloc just fails, or a line or two after malloc in execution where there is apparently no problem wrt code.
If malloc, free sequence is bad, or you're somewhere else writing to memory more than the amount allocated, then theres problem..
Just few cases I've listed down ... maybe something clicks...

Your application should exit gracefully in case MALLOC or ISHELL_CreateInstance fails. Always check if memory is allocated, and do further procesing with that memory or interface pointer, if allocated successfully.

Your application should exit gracefully in case MALLOC or ISHELL_CreateInstance fails. Always check if memory is allocated, and do further procesing with that memory or interface pointer, if allocated successfully.

Hi Taha,
Thanks for your reply.I am allocating around more than 1500 bytes on the heap during a function call and cleaning up the allocated memory within it.But that function is being called 6 times for a specific event.
I have a couple of questions regarding this issue:
1. Are there any chances of memory corruption due to repeated memory allocations and read/write operations??
2. How can i check how much heap memory is available, on the emulator??
Hi Oceans,
Thanks for your precious suggestions.
The MALLOC() function fails itself on the first statement and i am not getting any chance to validate the pointer whether the requested memory could be allocated properly or not.
AECHAR *pointerToMem = (AECHAR *)MALLOC(512 * sizeof(AECHAR));// The application crashes here.
if(pointerToMem == NULL)
{
return;

Can you please tell me whether i am falling short of memory or anything else?
Thanks in advance......... :) :)

Hi Taha,
Thanks for your reply.I am allocating around more than 1500 bytes on the heap during a function call and cleaning up the allocated memory within it.But that function is being called 6 times for a specific event.
I have a couple of questions regarding this issue:
1. Are there any chances of memory corruption due to repeated memory allocations and read/write operations??
2. How can i check how much heap memory is available, on the emulator??
Hi Oceans,
Thanks for your precious suggestions.
The MALLOC() function fails itself on the first statement and i am not getting any chance to validate the pointer whether the requested memory could be allocated properly or not.
AECHAR *pointerToMem = (AECHAR *)MALLOC(512 * sizeof(AECHAR));// The application crashes here.
if(pointerToMem == NULL)
{
return;

Can you please tell me whether i am falling short of memory or anything else?
Thanks in advance......... :) :)

akshay1384 wrote:
2. How can i check how much heap memory is available, on the emulator??
Open simulator. In 'Device details'->Memory->Heap Size', you can find the total amount of heap memory available on system
In application,
To get the current statistics about the total memory currently in use, IHEAP_GetMemStats() API must be used.
To check if a block of a specific size can be allocated, the function IHEAP_CheckAvail() must be called..

akshay1384 wrote:
2. How can i check how much heap memory is available, on the emulator??
Open simulator. In 'Device details'->Memory->Heap Size', you can find the total amount of heap memory available on system
In application,
To get the current statistics about the total memory currently in use, IHEAP_GetMemStats() API must be used.
To check if a block of a specific size can be allocated, the function IHEAP_CheckAvail() must be called..

Quote:1. Are there any chances of memory corruption due to repeated memory allocations and read/write operations??
....
....
....
AECHAR *pointerToMem = (AECHAR *)MALLOC(512 * sizeof(AECHAR));// The application crashes here.
if(pointerToMem == NULL)
{
return;

Perhaps you can post this particular function's code, and everyone can have a look at the problem..
Suggestion:
In memory allocation for strings (char* or AECHAR*), it is possible you try to copy buffer(of length for ex: 512), into your pointerToMem, by some string copy API call, where as it is allocated for size 512. So, the NULL terminating character tries to take the 513th memory location, which is obviously not available.
Try allocating one more AECHAR for NULL terminating character. Maybe it helps.
Example:
AECHAR *pointerToMem = (AECHAR *)MALLOC(512 * sizeof(AECHAR) + sizeof(AECHAR) );
if(pointerToMem == NULL)
{
return;

Quote:1. Are there any chances of memory corruption due to repeated memory allocations and read/write operations??
....
....
....
AECHAR *pointerToMem = (AECHAR *)MALLOC(512 * sizeof(AECHAR));// The application crashes here.
if(pointerToMem == NULL)
{
return;

Perhaps you can post this particular function's code, and everyone can have a look at the problem..
Suggestion:
In memory allocation for strings (char* or AECHAR*), it is possible you try to copy buffer(of length for ex: 512), into your pointerToMem, by some string copy API call, where as it is allocated for size 512. So, the NULL terminating character tries to take the 513th memory location, which is obviously not available.
Try allocating one more AECHAR for NULL terminating character. Maybe it helps.
Example:
AECHAR *pointerToMem = (AECHAR *)MALLOC(512 * sizeof(AECHAR) + sizeof(AECHAR) );
if(pointerToMem == NULL)
{
return;

Hi,
This could be also due to fragmentation. Your phone might have the requested free memory available but not in a single chunk. This could lead to a failed MALLOC call. This happens when you frequently allocate chunks and free it.
-Karthik

Hi,
This could be also due to fragmentation. Your phone might have the requested free memory available but not in a single chunk. This could lead to a failed MALLOC call. This happens when you frequently allocate chunks and free it.
-Karthik

Hi Karthik,
Thanks for your valueable suggestion. Yes its correct i am having a series of memory allocation and deallocation and doing it frequently. But how can i resolve the fragmentation issue? Can you please suggest me a solution, it would be definitely a great help.
Thanks a lot...... :)

Hi Karthik,
Thanks for your valueable suggestion. Yes its correct i am having a series of memory allocation and deallocation and doing it frequently. But how can i resolve the fragmentation issue? Can you please suggest me a solution, it would be definitely a great help.
Thanks a lot...... :)

Allocate a chunk during app startup and keep it in your app pointer and reuse it. Free the pointer when the app exits.
-Karthik

Allocate a chunk during app startup and keep it in your app pointer and reuse it. Free the pointer when the app exits.
-Karthik

Hi Karthik,
Thanks for the quick reply.I got a little point about the fragmentation issue that you indicated earlier and it is absolutely a very good point to keep in mind prior to develop a mobile application as there are lethal memory constraints. As per my understanding, please correct me if i am wrong, the fragmentation issues come into picture when we are trying to have a cluster of memory requests as a batch allocation at the same time and if we are requesting for a different set of memories each time (ofcourse bigger ones) and which could be an obvious factor to create holes among the memory chunks.
For your convenience and to understand the issue which i am facing, here are some of the skeleton stuffs, those are used for my application:
typedef struct _Mystruct
{
AECHAR *str1;
AECHAR *str2;
AECHAR *str3;
.
.
.
AECHAR *str10;
Mystruct;
// within some function i am allocating the memory for the above structure which is a pointer variable of my main Applet structure:
Let say
Mystruct *myStruct;
void SomeFunction()
{
int index;
myStruct = (Mystruct *)MALLOC(6 * sizeof(Mystruct));
if(myStruct == NULL)
{
return;
}
for(index = 0; index < 6; index++)
{
FillStructure(pointer* appletStrucct, index, &myStruct);
}

// This function is being called 6 times as above and with frequent allocations and deallocations.
void FillStructure(pointer to my Applet structure, some index, pointer indirection to myStruct)
{
AECHAR *buffer, *tempBuf;
buffer = (AECHAR *)MALLOC(512 * sizeof(AECHAR));
if(buffer == NULL)
{
return;
}
// Assume this buffer holds a string with a length of 512
// Allocate the buffer with a string of length 50
tempBuf = (AECHAR *)MALLOC(50 * sizeof(AECHAR));
if(tempBuf == NULL)
{
return;
}
// Break buffer into 10 pieces and populate the tempBuf with this
// Allocate memory as much as length of tempBuf + 1 for each of the strings within the structure above and populate each of them with tempBuf contents.
FREE(tempBuf);
FREE(buffer);
// The strings within the above structure will be cleaned later, as we need the data persistence, to be used by the rest of the application

So what i understood from the above code as per your indications is:
I am always allocating all the 10 strings of the structure for 6 times as above and each time with a different request based on the length of the tempBuf string and may be a bigger one every time as compare to the previous request, this could create the fragmentation issue i guess. Also i am also allocating two other strings with 512 bytes and 50 bytes respectively(but these could not be the customers for the fragmentation issues i guess as the lengths are fixed).
Now i have a couple of questions running in my mind..... :confused: :
1. Are these allocations with different memory requests creating the fragmentation problems?
2. If i will take a fixed length for allocating all the 10 strings (if i know the max length that all of these 10 strings can take for each of the 6 times as above), will this fragmentation problem be gone?
3. Will this have any kind of impact upon the ISHELL_CreateInstance() also?
Can you please help me out to dig out these issues?
Thanks in advance...... :)

Hi Karthik,
Thanks for the quick reply.I got a little point about the fragmentation issue that you indicated earlier and it is absolutely a very good point to keep in mind prior to develop a mobile application as there are lethal memory constraints. As per my understanding, please correct me if i am wrong, the fragmentation issues come into picture when we are trying to have a cluster of memory requests as a batch allocation at the same time and if we are requesting for a different set of memories each time (ofcourse bigger ones) and which could be an obvious factor to create holes among the memory chunks.
For your convenience and to understand the issue which i am facing, here are some of the skeleton stuffs, those are used for my application:
typedef struct _Mystruct
{
AECHAR *str1;
AECHAR *str2;
AECHAR *str3;
.
.
.
AECHAR *str10;
Mystruct;
// within some function i am allocating the memory for the above structure which is a pointer variable of my main Applet structure:
Let say
Mystruct *myStruct;
void SomeFunction()
{
int index;
myStruct = (Mystruct *)MALLOC(6 * sizeof(Mystruct));
if(myStruct == NULL)
{
return;
}
for(index = 0; index < 6; index++)
{
FillStructure(pointer* appletStrucct, index, &myStruct);
}

// This function is being called 6 times as above and with frequent allocations and deallocations.
void FillStructure(pointer to my Applet structure, some index, pointer indirection to myStruct)
{
AECHAR *buffer, *tempBuf;
buffer = (AECHAR *)MALLOC(512 * sizeof(AECHAR));
if(buffer == NULL)
{
return;
}
// Assume this buffer holds a string with a length of 512
// Allocate the buffer with a string of length 50
tempBuf = (AECHAR *)MALLOC(50 * sizeof(AECHAR));
if(tempBuf == NULL)
{
return;
}
// Break buffer into 10 pieces and populate the tempBuf with this
// Allocate memory as much as length of tempBuf + 1 for each of the strings within the structure above and populate each of them with tempBuf contents.
FREE(tempBuf);
FREE(buffer);
// The strings within the above structure will be cleaned later, as we need the data persistence, to be used by the rest of the application

So what i understood from the above code as per your indications is:
I am always allocating all the 10 strings of the structure for 6 times as above and each time with a different request based on the length of the tempBuf string and may be a bigger one every time as compare to the previous request, this could create the fragmentation issue i guess. Also i am also allocating two other strings with 512 bytes and 50 bytes respectively(but these could not be the customers for the fragmentation issues i guess as the lengths are fixed).
Now i have a couple of questions running in my mind..... :confused: :
1. Are these allocations with different memory requests creating the fragmentation problems?
2. If i will take a fixed length for allocating all the 10 strings (if i know the max length that all of these 10 strings can take for each of the 6 times as above), will this fragmentation problem be gone?
3. Will this have any kind of impact upon the ISHELL_CreateInstance() also?
Can you please help me out to dig out these issues?
Thanks in advance...... :)