IStatic to release alloc'ed buffer. | developer.brewmp.com IStatic to release alloc'ed buffer. | developer.brewmp.com

Developer

IStatic to release alloc'ed buffer.

Forums:

I have a code like this:

buff = (char *)MALLOC(sizeof(char)*1024);
Dialog(pApp, buff);
FREE(buff);

.
.
.

void Dialog(MyApplet *pApp, char *buff)
{
.
.
.
ISTATIC_SetProperties(pApp->pText, (ST_NOSCROLL|ST_ASCII|ST_TEXTALLOC));
ISTATIC_SetTextEx(pApp->pText, buff, NULL, FALSE);
.
.
.

But when the Static opens if I use some key to scroll the text It show a message like Memory corruption STRTOWSTR with a 5s counter..
If I havent the propertie ST_TEXTALLOC it works but it dont free the text buffer.

i don't think Istatic will free the buffer. Did you check the validation of buff before you set it as the source of text?

i don't think Istatic will free the buffer. Did you check the validation of buff before you set it as the source of text?

Yes, I do.. and its a valid char* buffer with contents and \0 at end.
Well, if IStatic wont will free my buffer, what is the ST_TEXTALLOC property?
Anybody here that uses ISTATIC has some sample working ok to show? Its very strange that it eats my mem.. its keeping a copy of my buffer internally cause Im sure that Im freeing everything mine.
[edit]
I tested ISTATIC with only a title, and no body.. and tried to create and destroy everytime about 10 times and it worked.. but when I have a text body when I try to release it to create again, It crashes.
ps.: Its only me or that api doc isnt very good???

Yes, I do.. and its a valid char* buffer with contents and \0 at end.
Well, if IStatic wont will free my buffer, what is the ST_TEXTALLOC property?
Anybody here that uses ISTATIC has some sample working ok to show? Its very strange that it eats my mem.. its keeping a copy of my buffer internally cause Im sure that Im freeing everything mine.
[edit]
I tested ISTATIC with only a title, and no body.. and tried to create and destroy everytime about 10 times and it worked.. but when I have a text body when I try to release it to create again, It crashes.
ps.: Its only me or that api doc isnt very good???

Here is my code:
[edit] I fixed this code, that it will work ok. When calling this function, dont free your buffer. ISTATIC will do it for you.
BA_BOOL oDialog(MyApplet pApp, uint16 nTitleID, AECHAR *title, char *file, BA_BOOL isfile)
{
AEERect rRect;
IFileMgr *pFileMgr;
IFile *pFile;
uint32 flags = 0;
SETAEERECT(&rRect,
BA_PLATFORM.WorkingRect.x,
BA_PLATFORM.WorkingRect.y,
BA_PLATFORM.WorkingRect.dx,
BA_PLATFORM.WorkingRect.dy-15
);
ISTATIC_Reset(pApp->pText);
ISTATIC_SetRect(pApp->pText, &rRect);
ISTATIC_SetProperties(pApp->pText, 0);
if (!isfile)
flags = ST_TEXTALLOC;
if (title)
{
ISTATIC_SetProperties(pApp->pText, (ST_CENTERTITLE|ST_UNDERLINE|ST_NOSCROLL|ST_ASCII|flags));
ISTATIC_SetText(pApp->pText, title, NULL, AEE_FONT_NORMAL, AEE_FONT_NORMAL);
}
else
{
ISTATIC_SetProperties(pApp->pText, (ST_NOSCROLL|ST_ASCII|flags));
}
if (isfile)
{
ISHELL_CreateInstance(pApp->pShell, AEECLSID_FILEMGR, (void**)(&pFileMgr));
if (pFileMgr)
{
pFile = IFILEMGR_OpenFile(pFileMgr, file, _OFM_READ);
BA_RELEASE(pFileMgr);
if (pFile)
{
ISTATIC_SetTextEx(pApp->pText, NULL, (IAStream*)pFile, FALSE);
BA_RELEASE(pFile);
}
else return BA_FALSE;
}
else return BA_FALSE;
}
else ISTATIC_SetTextEx(pApp->pText, (byte *)file, NULL, FALSE);
ISTATIC_Redraw(pApp->pText);
return BA_TRUE;

Considerations:
- At app init I create pApp->pText that is a ISTATIC Ctl..
- At app stop I release pApp->pText ...
obs1: In that line, if my code runs by the second time, it crashes... I only use one ISTATIC at time, and change the body text.. Im not using the file mode.. only direct "char *" buffer...
I need some urgent help with this... thanks

Here is my code:
[edit] I fixed this code, that it will work ok. When calling this function, dont free your buffer. ISTATIC will do it for you.
BA_BOOL oDialog(MyApplet pApp, uint16 nTitleID, AECHAR *title, char *file, BA_BOOL isfile)
{
AEERect rRect;
IFileMgr *pFileMgr;
IFile *pFile;
uint32 flags = 0;
SETAEERECT(&rRect,
BA_PLATFORM.WorkingRect.x,
BA_PLATFORM.WorkingRect.y,
BA_PLATFORM.WorkingRect.dx,
BA_PLATFORM.WorkingRect.dy-15
);
ISTATIC_Reset(pApp->pText);
ISTATIC_SetRect(pApp->pText, &rRect);
ISTATIC_SetProperties(pApp->pText, 0);
if (!isfile)
flags = ST_TEXTALLOC;
if (title)
{
ISTATIC_SetProperties(pApp->pText, (ST_CENTERTITLE|ST_UNDERLINE|ST_NOSCROLL|ST_ASCII|flags));
ISTATIC_SetText(pApp->pText, title, NULL, AEE_FONT_NORMAL, AEE_FONT_NORMAL);
}
else
{
ISTATIC_SetProperties(pApp->pText, (ST_NOSCROLL|ST_ASCII|flags));
}
if (isfile)
{
ISHELL_CreateInstance(pApp->pShell, AEECLSID_FILEMGR, (void**)(&pFileMgr));
if (pFileMgr)
{
pFile = IFILEMGR_OpenFile(pFileMgr, file, _OFM_READ);
BA_RELEASE(pFileMgr);
if (pFile)
{
ISTATIC_SetTextEx(pApp->pText, NULL, (IAStream*)pFile, FALSE);
BA_RELEASE(pFile);
}
else return BA_FALSE;
}
else return BA_FALSE;
}
else ISTATIC_SetTextEx(pApp->pText, (byte *)file, NULL, FALSE);
ISTATIC_Redraw(pApp->pText);
return BA_TRUE;

Considerations:
- At app init I create pApp->pText that is a ISTATIC Ctl..
- At app stop I release pApp->pText ...
obs1: In that line, if my code runs by the second time, it crashes... I only use one ISTATIC at time, and change the body text.. Im not using the file mode.. only direct "char *" buffer...
I need some urgent help with this... thanks

IStatic Demystified:
Internally, IStatic keeps its own buffer for title and text. When you do a set text, IStatic does a deep copy of the (char */AECHAR *) provided to it. However when the ST_TEXTALLOC/ST_TITLEALLOC properties are set, then IStatic just points to the (char*/AECHAR*) buffer provided (AND HENCE NO deep copy). Note however, that when IStatic_Release() is called, it will ALWAYS free the buffer.
What does this mean to you as a developer:
[INDENT]If you are NOT using ST_TEXTALLOC then you are responsible for freeing the buffer containing the text passed to SetText().[/INDENT]
[INDENT]If you are using ST_TEXTALLOC, do not free the text buffer. IStatic will free that buffer when you call ISTATIC_Release() (or when you do another SetText()). Be careful while using ST_TEXTALLOC - it is powerful when using dynamically changing text but you need to make sure that the buffer does not get corrupted/freed[/INDENT]

IStatic Demystified:
Internally, IStatic keeps its own buffer for title and text. When you do a set text, IStatic does a deep copy of the (char */AECHAR *) provided to it. However when the ST_TEXTALLOC/ST_TITLEALLOC properties are set, then IStatic just points to the (char*/AECHAR*) buffer provided (AND HENCE NO deep copy). Note however, that when IStatic_Release() is called, it will ALWAYS free the buffer.
What does this mean to you as a developer:
[INDENT]If you are NOT using ST_TEXTALLOC then you are responsible for freeing the buffer containing the text passed to SetText().[/INDENT]
[INDENT]If you are using ST_TEXTALLOC, do not free the text buffer. IStatic will free that buffer when you call ISTATIC_Release() (or when you do another SetText()). Be careful while using ST_TEXTALLOC - it is powerful when using dynamically changing text but you need to make sure that the buffer does not get corrupted/freed[/INDENT]

Very good explaination.. You can ask someone to put that on docs next release :)
I will do some changes here to test this.. Thank you!
Soon I post here if its work or not... :P
[edit]
One thing:
Quote:
IStatic will free that buffer when you call ISTATIC_Release() (or when you do another SetText()).
This will happen too if I call ISTATIC_Reset() ?

Very good explaination.. You can ask someone to put that on docs next release :)
I will do some changes here to test this.. Thank you!
Soon I post here if its work or not... :P
[edit]
One thing:
Quote:
IStatic will free that buffer when you call ISTATIC_Release() (or when you do another SetText()).
This will happen too if I call ISTATIC_Reset() ?

it's not Istatic free the memory. when you set "ST_TEXTALLOC", the Dialog will free the memory.
check the doc:
"ST_TEXTALLOC Text allocated on heap - dialog takes
responsibility of freeing it.
ST_TITLEALLOC Title allocated on heap - dialog takes
responsibility of freeing it."
because of this line:
ISTATIC_SetProperties(pApp->pText, (ST_NOSCROLL|ST_ASCII|ST_TEXTALLOC));
your dialog will free pApp->pText after it's done. this causes the crash at the second time.

it's not Istatic free the memory. when you set "ST_TEXTALLOC", the Dialog will free the memory.
check the doc:
"ST_TEXTALLOC Text allocated on heap - dialog takes
responsibility of freeing it.
ST_TITLEALLOC Title allocated on heap - dialog takes
responsibility of freeing it."
because of this line:
ISTATIC_SetProperties(pApp->pText, (ST_NOSCROLL|ST_ASCII|ST_TEXTALLOC));
your dialog will free pApp->pText after it's done. this causes the crash at the second time.

Thanks.. now its worked.

Thanks.. now its worked.

Yup, ISTATIC_Reset() will free the text buffer as well (note: not the tiltle buffer). If ST_TEXTALLOC has been set, then your external buffer will be freed. In addition, note that this will also reset any ST_TEXTALLOC/ST_TITLEALLOC properties among other things.

Yup, ISTATIC_Reset() will free the text buffer as well (note: not the tiltle buffer). If ST_TEXTALLOC has been set, then your external buffer will be freed. In addition, note that this will also reset any ST_TEXTALLOC/ST_TITLEALLOC properties among other things.