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:

ptr_memory:0xe600b0

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

// Example of hanging when reading a bad pointer

#include "mbed.h"

DigitalOut myled(LED1);

int x[10] = {0};

extern "C" void HardFault_Handler() {
    error("Hit HardFault handler!\n");
}

int main() {
    int *ok_ptr = x;
    int *bad_ptr = (int*)0xe600b0; // not in RAM!
    printf("ok_ptr = 0x%08X, bad_ptr = 0x%08X\n", ok_ptr, bad_ptr);
    
    int ok_read = ok_ptr[0];
    printf("ok_read = %d\n", ok_read);
    
    int bad_read = bad_ptr[0]; 
    printf("bad_read = %d\n", bad_read);

    while(1) {
        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);
    }
}

Hope that helps,

Simon

04 Mar 2012

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

Simon

04 Mar 2012

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

fread(&ptr_memory,1,size,fh);

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.