Random freeze up on writing to a pointer?

04 Mar 2012

Hello, I've been working on porting a library I wrote some time ago. It's a pretty involved project, but basically I'm stuck because I can't figure out why my mbed just freezes at this point. I am using the mbed NXP LPC1768 and the online compiler.

The code in question is simple:

    virtual void Read(uint32_t address,int count,void *buffer){
        pc.printf("memory read: 0x%x; count: %i\n",address,count);
        for(int i=0;i<count;i++){ //didn't use memcpy because I thought memory alignment may be an issue
            pc.printf("buffer: 0x%x; ptr_memory:0x%x\n",buffer,ptr_memory);
            ((uint8_t*)buffer)[i]=ptr_memory[address]; //freeze up here

The function is basically called like this:

uint32_t temp=0;
MyClass.Read(0, 4, &temp);
return temp;

ptr_memory is initialized like this:

uint8_t *ptr_memory=(uint8_t*)malloc(32);

I've triple checked all my code, but I can't reproduce this in a simple test case.

Are there any problems that I could have writing to a stack-allocated place like I am? This is the only thing I can think of. I'm not familiar with the low-level portions of ARM and what differences there are between it and the traditional x86.

Does anyone have any ideas?

Also, not sure if it'll help anyone, but the pointer addresses are as so:

buffer: 0x10007fb0; ptr_memory:0xe600b0
04 Mar 2012

What happens if you do use memcpy?

04 Mar 2012

Hi Jordan,

Something doesn't smell right to me:


That wouldn't be in RAM (which starts at 0x10000000, see the User Manual on the mbed NXP LPC1768 handbook page for full memory map), so looks like an uninitialised or incorrect pointer.

This example shows the behaviour, firing the error (so you get the Blue Lights of Death!):

Import program

00001 // Example of hanging when reading a bad pointer
00003 #include "mbed.h"
00005 DigitalOut myled(LED1);
00007 int x[10] = {0};
00009 extern "C" void HardFault_Handler() {
00010     error("Hit HardFault handler!\n");
00011 }
00013 int main() {
00014     int *ok_ptr = x;
00015     int *bad_ptr = (int*)0xe600b0; // not in RAM!
00016     printf("ok_ptr = 0x%08X, bad_ptr = 0x%08X\n", ok_ptr, bad_ptr);
00018     int ok_read = ok_ptr[0];
00019     printf("ok_read = %d\n", ok_read);
00021     int bad_read = bad_ptr[0]; 
00022     printf("bad_read = %d\n", bad_read);
00024     while(1) {
00025         myled = 1;
00026         wait(0.2);
00027         myled = 0;
00028         wait(0.2);
00029     }
00030 }

Hope that helps,


04 Mar 2012

I might consider putting error messages in the fault handlers by default if that sounds useful?


04 Mar 2012

Heh even after triple checking my pointers I found it was a pointer error. The error was this line of code:


Oddly enough not causing a casting warning. So you can move on with your day now. I spent 3 hours checking everything and it ended up being in an easy to see spot that I just overlooked. Gotta love pointer problems lol

Also, thanks for the advice about a hard fault handler. That will come in handy and I had been meaning to look into how to implement one

Please log in to post a reply.