variables and memory leaks | developer.brewmp.com variables and memory leaks | developer.brewmp.com

Developer

variables and memory leaks

Forums:

which type of variables is more likely to cause a memory leak?

emilio wrote:which type of variables is more likely to cause a memory leak?
Pointers :)
Seriously though, the only way to get a memory leak is to MALLOC memory that you do not FREE, or create a new object that you do not delete (plus a few things to do with BREW resources e.g. IIMAGE, area of memory resulting from CONVERTBMP, etc). Why do you ask, just out of interest?

emilio wrote:which type of variables is more likely to cause a memory leak?
Pointers :)
Seriously though, the only way to get a memory leak is to MALLOC memory that you do not FREE, or create a new object that you do not delete (plus a few things to do with BREW resources e.g. IIMAGE, area of memory resulting from CONVERTBMP, etc). Why do you ask, just out of interest?

Emilio,
I am not sure you understand the concept of memory leaks. Someone refers to a memory leak when you have an area of memory that has been allocated for use but is never released and therefore the memory is lost to the application, gradually shrinking the available memory pool.
As you can certainly tell by now, this behavior is entirely unrelated to any data type in particular. It is a matter of the program design, and as simon points out when you allocate memory - whether that's through malloc, new or any other function that allocates memory - you wil have to make sure you are properly releasing all the memory these functions allocate in order to avoid memory leaks.

Emilio,
I am not sure you understand the concept of memory leaks. Someone refers to a memory leak when you have an area of memory that has been allocated for use but is never released and therefore the memory is lost to the application, gradually shrinking the available memory pool.
As you can certainly tell by now, this behavior is entirely unrelated to any data type in particular. It is a matter of the program design, and as simon points out when you allocate memory - whether that's through malloc, new or any other function that allocates memory - you wil have to make sure you are properly releasing all the memory these functions allocate in order to avoid memory leaks.

In this function several pointer are declared locally, there are only a free function for szbuf, now my my quetion is if all these pointers can cause a memory leak?
static void DisplayOutput(IShell *pIShell, IDisplay *pIDisplay, int nline, char *pszStr)
{
AEEDeviceInfo di; // Device Info
AECHAR * szBuf;
AECHAR * psz = NULL;
int charHeight = 0; // Stores the char height in pixels for given font
int pnAscent = 0; // Stores the ascent in number of pixels
int pnDescent = 0; // Stores the descent in number of pixels
int pixelWidth;
AEEFont font = AEE_FONT_NORMAL;
int pnFits = 0, dy;
int totalCh = 0;
// Make sure the pointers we'll be using are valid
if (pIShell == NULL || pIDisplay == NULL)
return;
if ((szBuf = (AECHAR *) MALLOC(TEXT_BUFFER_SIZE)) == NULL)
return;
// Get device information
ISHELL_GetDeviceInfo(pIShell,&di);
// Get the font metrics info
charHeight = IDISPLAY_GetFontMetrics (pIDisplay, AEE_FONT_NORMAL,
&pnAscent, &pnDescent);
// Convert to wide string (unicode)
STR_TO_WSTR ((char *)pszStr, szBuf, TEXT_BUFFER_SIZE);
// If nlines is zero then print this string starting around the middle of
// the screen. Or else multiply nlines by charheight to decide the y coordinate of
// the start of the string.
if (nline < 0) {
dy = di.cyScreen*2/5;
}
else{
dy = nline * charHeight;
}
// psz keeps track of the point from which to write from the string buffer
// in case the string does not fit one line and needs to wrap around in the
// next line.
psz = szBuf;
// Need to calculate the lotal string length to decide if any wrapping
// around is needed.
totalCh = STRLEN ((char *)pszStr);
// Keep displaying text string on multiple lines if the string can't be displayed
// on one single line. Lines are spaced 15 pixels apart.
while ((totalCh > 0) && (*psz != NULL))
{
// Get information on how many characters will fit in a line.
// Give the pointer to the buffer to be displayed, and the number of
// pixels along the x axis you want to display the string in (max number)
// pnFits will have the max number of chars that will fit in the maxWidth
// number of pixels (given string can't fit in one line), or the number of
// chars in the string (if it does fit in one line). pnWidth gives the
// number of pixels that will be used to display pnFits number of chars.
pixelWidth = IDISPLAY_MeasureTextEx(pIDisplay,
font,
(AECHAR *) psz, // Start of the buffer to display,
-1,
di.cxScreen - 5, // maxWidth
&pnFits); // Number of chars that will fit a line
// If pnFits is zero there is something wrong in the input to above function.
// Normally this scenario should not occur. But, have the check anyway.
if (pnFits == 0)
{
FREE(szBuf);
return;
}
IDISPLAY_DrawText(pIDisplay, AEE_FONT_NORMAL, psz, pnFits, 5 /*start dx*/,
dy, 0 /* use default rectangle coordinates */, 0);
psz += pnFits; // move pointer to the next segment to be displayed
totalCh -= pnFits; // reduce the total number of characters to still display
dy += charHeight; // Place next line charHeight pixels below the
// previous line.
IDISPLAY_Update(pIDisplay); //, TRUE);
if (totalCh < pnFits)
pnFits = totalCh; // if total number is less than pnFits, adjust pnFits
}
FREE(szBuf);
return;
} // End of DisplayOutput

In this function several pointer are declared locally, there are only a free function for szbuf, now my my quetion is if all these pointers can cause a memory leak?
static void DisplayOutput(IShell *pIShell, IDisplay *pIDisplay, int nline, char *pszStr)
{
AEEDeviceInfo di; // Device Info
AECHAR * szBuf;
AECHAR * psz = NULL;
int charHeight = 0; // Stores the char height in pixels for given font
int pnAscent = 0; // Stores the ascent in number of pixels
int pnDescent = 0; // Stores the descent in number of pixels
int pixelWidth;
AEEFont font = AEE_FONT_NORMAL;
int pnFits = 0, dy;
int totalCh = 0;
// Make sure the pointers we'll be using are valid
if (pIShell == NULL || pIDisplay == NULL)
return;
if ((szBuf = (AECHAR *) MALLOC(TEXT_BUFFER_SIZE)) == NULL)
return;
// Get device information
ISHELL_GetDeviceInfo(pIShell,&di);
// Get the font metrics info
charHeight = IDISPLAY_GetFontMetrics (pIDisplay, AEE_FONT_NORMAL,
&pnAscent, &pnDescent);
// Convert to wide string (unicode)
STR_TO_WSTR ((char *)pszStr, szBuf, TEXT_BUFFER_SIZE);
// If nlines is zero then print this string starting around the middle of
// the screen. Or else multiply nlines by charheight to decide the y coordinate of
// the start of the string.
if (nline < 0) {
dy = di.cyScreen*2/5;
}
else{
dy = nline * charHeight;
}
// psz keeps track of the point from which to write from the string buffer
// in case the string does not fit one line and needs to wrap around in the
// next line.
psz = szBuf;
// Need to calculate the lotal string length to decide if any wrapping
// around is needed.
totalCh = STRLEN ((char *)pszStr);
// Keep displaying text string on multiple lines if the string can't be displayed
// on one single line. Lines are spaced 15 pixels apart.
while ((totalCh > 0) && (*psz != NULL))
{
// Get information on how many characters will fit in a line.
// Give the pointer to the buffer to be displayed, and the number of
// pixels along the x axis you want to display the string in (max number)
// pnFits will have the max number of chars that will fit in the maxWidth
// number of pixels (given string can't fit in one line), or the number of
// chars in the string (if it does fit in one line). pnWidth gives the
// number of pixels that will be used to display pnFits number of chars.
pixelWidth = IDISPLAY_MeasureTextEx(pIDisplay,
font,
(AECHAR *) psz, // Start of the buffer to display,
-1,
di.cxScreen - 5, // maxWidth
&pnFits); // Number of chars that will fit a line
// If pnFits is zero there is something wrong in the input to above function.
// Normally this scenario should not occur. But, have the check anyway.
if (pnFits == 0)
{
FREE(szBuf);
return;
}
IDISPLAY_DrawText(pIDisplay, AEE_FONT_NORMAL, psz, pnFits, 5 /*start dx*/,
dy, 0 /* use default rectangle coordinates */, 0);
psz += pnFits; // move pointer to the next segment to be displayed
totalCh -= pnFits; // reduce the total number of characters to still display
dy += charHeight; // Place next line charHeight pixels below the
// previous line.
IDISPLAY_Update(pIDisplay); //, TRUE);
if (totalCh < pnFits)
pnFits = totalCh; // if total number is less than pnFits, adjust pnFits
}
FREE(szBuf);
return;
} // End of DisplayOutput

No, the way I see it they cannot leak. You are allocating one buffer and you're releasing it before exiting the function.

No, the way I see it they cannot leak. You are allocating one buffer and you're releasing it before exiting the function.

Hi emilio,
Thats the very nice piece of code to wrap the text!
Kudos to you for posting such helpful snippet!
Regds,
Avinash

Hi emilio,
Thats the very nice piece of code to wrap the text!
Kudos to you for posting such helpful snippet!
Regds,
Avinash

hello emilio,
they are so nice~~!
thanks for your pointer!!

hello emilio,
they are so nice~~!
thanks for your pointer!!