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

Developer

Forums

Forums:

Hi all,

I was wondering if anyone could help out. I'm trying to learn about HTTP connections and was looking at the NetDiagnostics example. What I did was copy parts of it into my own code so I could play around with it. Now, it works fine with in the simulator but resets on the phone. I've narrowed it down to the Print function

For example

PrintMe(pwa->pParent, "%s", pszStatus);

will reset the phone but removing the "%s" will be ok i.e.

PrintMe(pwa->pParent, pszStatus);

I copied the print function exactly, but renamed the variables for my understanding

static void PrintMe(brewtest* pMe, char* pszFmt, ...)
{
char buf[512];
va_list argptr;
va_start(argptr, pszFmt);

if(pMe->pIStatic)
{
if (VSNPRINTF(buf, sizeof(buf), pszFmt, argptr) >= 0)
{
ISTATIC_SetTextEx(pMe->pIStatic, (byte*)buf, NULL, TRUE);
ISTATIC_Redraw(pMe->pIStatic);
}
}
va_end(argptr);

Does anyone have a clue as to why this might be? Thanks

could be a stack overflow issue

could be a stack overflow issue

Thanks for the reply. I'm not doing much but if it is a stack overflow like you said then the stack is quite small. I've searched on the forums, but couldn't find out how we find out the stack size. Can anyone provide a code sample?

Thanks for the reply. I'm not doing much but if it is a stack overflow like you said then the stack is quite small. I've searched on the forums, but couldn't find out how we find out the stack size. Can anyone provide a code sample?

it can be quite small, if memory serves as small as 1K on some phones
a simple test would be to allocate the temp string with MALLOC and FREE instead of putting it on the stack, if it still crashes, its not stack related.

it can be quite small, if memory serves as small as 1K on some phones
a simple test would be to allocate the temp string with MALLOC and FREE instead of putting it on the stack, if it still crashes, its not stack related.

Thanks charliex, I did something like this:
char* content
content = MALLOC(sizeof(char)*512);
STRCPY(content, " testing testing");
....
FREE(content);
which reset the phone but when I made content a global variable it didn't reset, which would mean that it's probably a stack overflow problem, right? Ha ha, I'm so not used to thinking like this. Programming under BREW will probably take a long time :(
Quick question though. Is the struct that you define for your class allocated on the stack or heap? I just realized I have a lot of variables here, which could be the reason why it keeps resetting the phone.

Thanks charliex, I did something like this:
char* content
content = MALLOC(sizeof(char)*512);
STRCPY(content, " testing testing");
....
FREE(content);
which reset the phone but when I made content a global variable it didn't reset, which would mean that it's probably a stack overflow problem, right? Ha ha, I'm so not used to thinking like this. Programming under BREW will probably take a long time :(
Quick question though. Is the struct that you define for your class allocated on the stack or heap? I just realized I have a lot of variables here, which could be the reason why it keeps resetting the phone.

It depends how you are allocating your class/struct, if using new/MALLOC then on heap.

It depends how you are allocating your class/struct, if using new/MALLOC then on heap.

if you made content a global are you using gcc or the postprocessor tool to allow you to use globals ?
smells of stack corruption but content would only take 4 bytes. so it could be a buffer/stack overflow too.
did you change the PrintMe routine to allocate from the heap instead?

if you made content a global are you using gcc or the postprocessor tool to allow you to use globals ?
smells of stack corruption but content would only take 4 bytes. so it could be a buffer/stack overflow too.
did you change the PrintMe routine to allocate from the heap instead?

@Ruben, thanks for the reply. I'm allocating the variables in the struct under the template the wizard gives. Since there's no new/MALLOC statement I'm assuming that it's on the stack.
@charliex, thanks for the feedback.
- if you made content a global are you using gcc or the postprocessor tool to allow you to use globals ?
I didn't know that I had to use gcc or the postprocessor tool to use globals. My understanding of globals was to create variables outside function definitions. So I did something like this:
struct _brewtest{
...
...
brewtest;
static char* content; //<---- I assumed that these were global variables
static char* Host;
static char* MSG;
// function prototypes
...
...
- did you change the PrintMe routine to allocate from the heap instead?
I don't really know how to change the PrintMe routine. This was a cut and paste from the NetDiagnostics example. I had an idea of moving the char buf[512] outside of the PrintMe, but that didn't work. But, if I'm not correctly creating global variables, this could be the case.

@Ruben, thanks for the reply. I'm allocating the variables in the struct under the template the wizard gives. Since there's no new/MALLOC statement I'm assuming that it's on the stack.
@charliex, thanks for the feedback.
- if you made content a global are you using gcc or the postprocessor tool to allow you to use globals ?
I didn't know that I had to use gcc or the postprocessor tool to use globals. My understanding of globals was to create variables outside function definitions. So I did something like this:
struct _brewtest{
...
...
brewtest;
static char* content; //<---- I assumed that these were global variables
static char* Host;
static char* MSG;
// function prototypes
...
...
- did you change the PrintMe routine to allocate from the heap instead?
I don't really know how to change the PrintMe routine. This was a cut and paste from the NetDiagnostics example. I had an idea of moving the char buf[512] outside of the PrintMe, but that didn't work. But, if I'm not correctly creating global variables, this could be the case.

Dependant on toolset/compiler brew doesn't natively support file or global scoped variables, it does support globally or file scoped constants.
static char*.... would be a file scoped variable and thus unsupported without the right compiler/post processor. if your code has lots of these, thats probably why its crashing as you'll be overwriting areas of memory you shouldn't be.
#define TMP_BUFSIZE ( 512 )
static void PrintMe(brewtest* pMe, char* pszFmt, ...)
{
char*buf;
buf = (char *) MALLOC ( TMP_BUFSIZE ) ;
if (buf == NULL ) return;
va_list argptr;
va_start(argptr, pszFmt);
if(pMe->pIStatic)
{
if (VSNPRINTF(buf, TMP_BUFSIZE-1, pszFmt, argptr) >= 0)
{
ISTATIC_SetTextEx(pMe->pIStatic, (byte*)buf, NULL, TRUE);
ISTATIC_Redraw(pMe->pIStatic);
}
}
va_end(argptr);
FREE( buf ):
}
something like that.

Dependant on toolset/compiler brew doesn't natively support file or global scoped variables, it does support globally or file scoped constants.
static char*.... would be a file scoped variable and thus unsupported without the right compiler/post processor. if your code has lots of these, thats probably why its crashing as you'll be overwriting areas of memory you shouldn't be.
#define TMP_BUFSIZE ( 512 )
static void PrintMe(brewtest* pMe, char* pszFmt, ...)
{
char*buf;
buf = (char *) MALLOC ( TMP_BUFSIZE ) ;
if (buf == NULL ) return;
va_list argptr;
va_start(argptr, pszFmt);
if(pMe->pIStatic)
{
if (VSNPRINTF(buf, TMP_BUFSIZE-1, pszFmt, argptr) >= 0)
{
ISTATIC_SetTextEx(pMe->pIStatic, (byte*)buf, NULL, TRUE);
ISTATIC_Redraw(pMe->pIStatic);
}
}
va_end(argptr);
FREE( buf ):
}
something like that.

Thanks again for the feedback charliex, it kind of makes sense to me :D Just to let you know that I'm using the GNU compiler to make the make files and using the command prompt to generate the mod. nmake -f makefile.mak -all
I also tried your suggestion for allocating the PrintMe to heap but for some reason it doesn't write the entire string out to the screen, only the first 2 characters of the string. Will try a few more things out ;)

Thanks again for the feedback charliex, it kind of makes sense to me :D Just to let you know that I'm using the GNU compiler to make the make files and using the command prompt to generate the mod. nmake -f makefile.mak -all
I also tried your suggestion for allocating the PrintMe to heap but for some reason it doesn't write the entire string out to the screen, only the first 2 characters of the string. Will try a few more things out ;)

Ok after a lot of reading around the forums I've managed to solve the problem when I read this post:
http://brewforums.qualcomm.com/showthread.php?t=10032
This was probably why the mod file provided in the examples worked, but the mod file I created didn't.

Ok after a lot of reading around the forums I've managed to solve the problem when I read this post:
http://brewforums.qualcomm.com/showthread.php?t=10032
This was probably why the mod file provided in the examples worked, but the mod file I created didn't.