For Programmers: Free Programming Magazines  


Home > Archive > Smartphone Developer Forum > December 2005 > FREE() Crashing









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author FREE() Crashing
Vishuonline

2005-12-15, 7:05 pm

Hi Folks,

I am facing a problem with CRT function call free() to dellocate memory I am
allocating by calloc call. My device is Orange SPV e200 Smartphone.
My free() call successfully deallocates the memory intermittently. i mean if
I run the application.. say 3 times. It deallocates 2 times.. at times.. only
1... And .. it gets better.. when it DOES NOT execute successfully.. its
CRASHES the APP.
I always check the pointer to be NOT NULL, before sending it to free() call.
Like
if(myPtr != NULL){free(myPtr);myPtr = NULL;}

I dont know how to address this issue. I am not able to do any on-device
debugging with this device,, for some strange reason. Whenever I launch the
app for on-device debugging.... the break point is never reached and the IDE
dies on me. Ok.. on-device debuggign.. problem apart.. I need some help on
the APP-CRASH on free() call.

Best,
Alex Feinman (MVP)

2005-12-16, 3:59 am

Vishuonline wrote:
> Hi Folks,
>
> I am facing a problem with CRT function call free() to dellocate memory I am
> allocating by calloc call. My device is Orange SPV e200 Smartphone.
> My free() call successfully deallocates the memory intermittently. i mean if
> I run the application.. say 3 times. It deallocates 2 times.. at times.. only
> 1... And .. it gets better.. when it DOES NOT execute successfully.. its
> CRASHES the APP.
> I always check the pointer to be NOT NULL, before sending it to free() call.
> Like
> if(myPtr != NULL){free(myPtr);myPtr = NULL;}
>
> I dont know how to address this issue. I am not able to do any on-device
> debugging with this device,, for some strange reason. Whenever I launch the
> app for on-device debugging.... the break point is never reached and the IDE
> dies on me. Ok.. on-device debuggign.. problem apart.. I need some help on
> the APP-CRASH on free() call.
>
> Best,


Check that the pointer you pass to free() is the same you got from
calloc(), i.e. it did not get overwritten
Vishuonline

2005-12-16, 7:08 pm

Yes. I checked that one too.. All the pointers are unique place holders. And
there are on threads in the picture. Infact. I even check and display the
content inside the buffer, before calling free(),, it Still crashes... Any
clues??
Example code:
//////////////////////////////////////////////////////////
char * fName = (char *)calloc(strlen(colonTOKEN)-2,sizeof(char));
strcpy(fName,colonTOKEN+2);
TCHAR * fNameBuffer = (TCHAR *)calloc(strlen(fName),sizeof(TCHAR));
wsprintf(fNameBuffer,_T("%S"),fName);
TimedMessageBox(hwnd,fNameBuffer,_T("FULLNAME Debug Message"),MB_OK,1500);
pContact->put_FirstName(fNameBuffer);//MAIN OPERATION
TimedMessageBox(hwnd,_T("ADDED"),_T("APPLICATION DEBUG"),MB_OK,1500);
//IT NEVER DISPLAYS THE "GOING TO NEXTTOKEN" MESSAGE BOX
if(fName != NULL && strlen(fName))
{
free(fName);//IT CRASHES HERE
fName = NULL;
}
if(fNameBuffer != NULL && wcslen(fNameBuffer))
{
free(fNameBuffer);
fNameBuffer = NULL;
}

TimedMessageBox(hwnd,_T("GOING TO NEXTTOKEN"),_T("APPLICATION
DEBUG"),MB_OK,1500);
//////////////////////////////////////////////////////////

Vishu

"Alex Feinman (MVP)" wrote:

> Vishuonline wrote:
>
> Check that the pointer you pass to free() is the same you got from
> calloc(), i.e. it did not get overwritten
>

Vishuonline

2005-12-16, 7:08 pm

Hmm.. I think I might have solved the issue.. I think the order of free()
calls is playing spoil sport.. This issue is related to fragmentation support
in heap memory..
I read that.. smartphone DOES support fragmentation.. which.. i believe
should allow free() calls.. irrespective of the order or calloc/malloc calls.
I dont know.. why the current behavior occurs.... but.. atleast.. its works
fine.. if i set the order right..

Thanks!

"Alex Feinman (MVP)" wrote:

> Vishuonline wrote:
>
> Check that the pointer you pass to free() is the same you got from
> calloc(), i.e. it did not get overwritten
>

Michael J. Salamone

2005-12-17, 7:03 pm

The error is not in the CRT or fragmentation or order of frees.

The error is a heap corruption, calling free multiple times on the same
pointer, or some other bug which is putting the heap in a corrupt or
inconsistent state. The corruption occured *before* the call to free that
tanks.

You might sprinkle some calls to HeapValidate in your code to help narrow
down where the problem is.

If you want a tool that *might* help, check the link below.

--
Michael Salamone [eMVP]
Entrek Software, Inc.
www.entrek.com


"Vishuonline" <Vishuonline@discussions.microsoft.com> wrote in message
news:8DF44704-0F45-4C1B-89FC-3B83D8996C7D@microsoft.com...[color=darkred]
> Hmm.. I think I might have solved the issue.. I think the order of free()
> calls is playing spoil sport.. This issue is related to fragmentation
> support
> in heap memory..
> I read that.. smartphone DOES support fragmentation.. which.. i believe
> should allow free() calls.. irrespective of the order or calloc/malloc
> calls.
> I dont know.. why the current behavior occurs.... but.. atleast.. its
> works
> fine.. if i set the order right..
>
> Thanks!
>
> "Alex Feinman (MVP)" wrote:
>


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com