• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1825
  • Last Modified:

Windows CE 4.2 gives "0xC0000005: Access Violation" whenever GlobalMemoryStatus(... is called.

Hello.

I'm writing a C app running on a Win CE 4.2 mobile scanner device (Symbol MC1000).   The developement environment is embedded Visual C++.  I havn't had any trouble until recently.  I'm adding code to add/change records to a database that resides in the object store.  A "0xC0000005: access violation" error occurs when trying to call a local function.  The error is normally sporadic.   However; I am able to always generate the error when calling "GlobalMemoryStatus".    Any Ideas on what is causing this?   I'm putting all my time and effort into researching this problem and I'm not finding much.     Thanks.
0
wfuller6
Asked:
wfuller6
  • 4
  • 3
  • 3
1 Solution
 
PaulCaswellCommented:
Hi wfuller6,

You have almost certainly corrupted your heap.

a) Sprinkle calls to GlobalMemoryStatus all over the place and track which one causes the access violation. The problem is somewhere between the previous all and the one that crashed.

b) Use a debug heap system to catch null pointer access and bounds violations.

Paul
0
 
fridomCommented:
PaulCaswell has some very good points, you are accessing a NULL pointer somewhere. So you should start stripping down you program. I know this is quite a lot of work, but there is no way around it.

What you can try is to find a test case which shows the problem and debug that if the problem is "small" enough.

Regards
Friedrich
0
 
wfuller6Author Commented:
I might have found the problem. Below is the code.  The last line I replaced with free(pRecord);
You might spot something else that doesn't look right.   After I made the change I've tested without any problems.
Thanks.

      PCEPROPVAL pRecord,pRecPtr,pRecPtr2;
      TCHAR *pPtr;

      nLen=(sizeof(CEPROPVAL)*NUM_LOGIN_DB_PROPS)+(66*sizeof(WCHAR))+12; //66 chars + 12 null terms
      /*allocate buffer to hold array of 12 properties with all strings taking up 66 bytes in memory + null term chars*
      pRecPtr=LocalAlloc(LMEM_FIXED,nLen);

      /*Copy the data from the record structur to the
      marshaled data block with structure at front and data in back*/

      pRecord=pRecPtr;
      pPtr=(TCHAR*)((LPBYTE)pRecPtr+(sizeof(CEPROPVAL)*NUM_LOGIN_DB_PROPS));

      memset(pRecPtr,0,LocalSize(pRecPtr)); //zero structure

      pRecPtr->propid=PID_LOGIN_TRUCK;
      pRecPtr->val.lpwstr=pPtr;
      wcscpy(pPtr,loginRec.truck);
      pPtr+=lstrlen(pPtr)+1;
      pRecPtr++;

        .....the previous 5 lines of code are repeated 11 more times for other record properties

      pRecPtr=NULL;
      pPtr=NULL;
        free(pRecPtr);    //should be free(pRecord);
0
Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

 
PaulCaswellCommented:
Thats it! That would certainly cause the problem you are seeing.

Paul
0
 
wfuller6Author Commented:
As a side note:

//replaced this code that was always giving me a 0xC0000005 access violation error
LPMEMORYSTATUS lstat;
GlobalMemoryStatus(lstat);

//with this code. no more errors. not sure why.
MEMORYSTATUS stat;
GlobalMemoryStatus(&stat);
0
 
fridomCommented:
Well that's not suprising. Obviously GlobalMemoryStatus wants to write something into stat, you handed it over an uninitialzed Pointer, with could not hold all the needed space for the object witht the second approach you have given the proper amount of space.

So it was indeed an uninitialzed Pointer.

Regards
Friedrich
0
 
wfuller6Author Commented:
Thanks Friedrich.
I just had a moment of clarity. Though I'm  Relatively new to C.
I was confusing a pointer with the base address of a structure.

lstat     is not the same as      &stat

//I could have done this although not necessary
LPMEMORYSTATUS lstat;
MEMORYSTATUS stat;
lstat=&stat;
GlobalMemoryStatus(lstat);

This should fix other problems I'm having.
Thanks again.  I'm done with this issue
0
 
fridomCommented:
Alternativly the following would work:

LPMEMORYSTATUS lstat = malloc(sizeof(lstat));

But that's another story I guess ;-)

Regards
Friedrich
0
 
PaulCaswellCommented:
>>LPMEMORYSTATUS lstat = malloc(sizeof(lstat));
I suspect you meant to use:

LPMEMORYSTATUS lstat = malloc(sizeof(*lstat));

:-)
0
 
fridomCommented:
yeah, seems I missed just a few bytes , so say hello to the next crash ;-(

Regards
Friedrich
0

Featured Post

Will You Be GDPR Compliant by 5/28/2018?

GDPR? That's a regulation for the European Union. But, if you collect data from customers or employees within the EU, then you need to know about GDPR and make sure your organization is compliant by May 2018. Check out our preparation checklist to make sure you're on track today!

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now