1

I have to following function

    public int[] PollAllDevices()
    {
        /* Polling commands */
        const byte POLL_FRONT_RADAR = 0x00;
        const byte POLL_REAR_RADAR  = 0x01;
        const byte POLL_DISP        = 0x02;
        const byte POLL_CTRL        = 0x03;
        const byte POLL_EXP1        = 0x04;
        const byte POLL_EXP2        = 0x05;

        byte[] buff = new byte[32];
        int[] _ret = new int[32];// { 23, 24, 25, 26, 27, 28, 29, 30 };
        int i = 0;

        if(API.uart_write(devPtr, new byte[1] { POLL_FRONT_RADAR }, 0, 1))
        {
            if (API.uart_read(devPtr, buff, 0, 1, 1000) != 0) 
            {
                _ret[i++] = (int)buff[1];
            }
        }
        if (API.uart_write(devPtr, new byte[1] { POLL_REAR_RADAR }, 0, 1))
        {
            if (API.uart_read(devPtr, buff, 0, 1, 1000) != 0)
            {
                _ret[i++] = (int)buff[1];
            }
        }

        return _ret;

    }

I have _ret in the watch window. I can see it exists up until the point I call API.uart_read and then it turns grey in the watch window and I see

_ret Cannot obtain value of local or argument '_ret' as it is not available at this instruction pointer, possibly because it has been optimized away. int[]

(Just throwing that in there as extra information. It's not my question so I'm not sure why this was marked as a duplicate. All the solutions to that error seem to revolve around optimization and I don't have optimization turned on.)

When I try to write to it I get an array out of bounds exception. i=0 at that point but just for grins I have hardcoded it to _ret[0] and I still get the exception. If I change _ret to an int I can write to it again.

I have optimization turned off. Can anyone tell me what I am doing wrong here?

lusher00
  • 678
  • 1
  • 7
  • 18
  • Possible duplicate of [Cannot obtain value of local or argument as it is not available at this instruction pointer, possibly because it has been optimized away](http://stackoverflow.com/questions/8311303/cannot-obtain-value-of-local-or-argument-as-it-is-not-available-at-this-instruct) – Sinatr May 19 '16 at 13:42
  • @BackDoorNoBaby, I think the OP is saying that `_ret` seems to disappear (from the debugger) *within* `PollAllDevices`, after the `API.uart_read` call. – adv12 May 19 '16 at 13:45

4 Answers4

0

It is possible that (int)buff[1] is what is causing the out of bounds exception. Try splitting up _ret[i++] = (int)buff[1];into

int tempVar = (int)buff[1];
_ret[i++] = tempVar;

and see what happens.

lucasvw
  • 1,345
  • 17
  • 36
0

Can't really see why your array would fall out of scope for some reason, but I have a workaound in case no other solution comes up:

Make you method public void PollAllDevices(out _ref){...}, and use it like

int[] _ref;
PollAllDevies(out _ref);

And change the int[] _ret = new int[32]; inside to say _ref = new int[32].

Of course now you could also put a bool as return value (or some other meta data), if you have a use for that.

Squirrelkiller
  • 2,575
  • 1
  • 22
  • 41
0

A few thoughts...

I don't have access to your API, so I took your code and modified it as follows:

byte[] buff = new byte[32];
int[] _ret = new int[32];// { 23, 24, 25, 26, 27, 28, 29, 30 };
int i = 0;

if (true)
{
    if (true)
    {
        _ret[i++] = (int)buff[1];
    }
}
if (true)
{
    if (true)
    {
        _ret[i++] = (int)buff[1];
    }
}

This of course works just fine. If it were me I would re-introduce the calls to API.uart_write and see what happens. I would then remove those calls again and re-introduce the calls to API.uart_read. This will ensure it isn't related to the interaction of those two calls.

The code you posted does not appear to be complete because I can't see anywhere how devPtr is declared. Is it a second reference to _ret or one of _ret's values by any chance?

It doesn't even make sense that it would have been optimized away because you return it at the end of the method call. The compiler won't optimize away the return value of a method.

Craig W.
  • 17,838
  • 6
  • 49
  • 82
  • I have tried to move the call up above uart_read to its call immediately after the uart_write call and things are OK. Things also work when I remove the call to uart_write and only call uart_read. I'm really not sure what to do with that though. I really can't see what it is about calling those functions from my DLL is causing things to fall apart. – lusher00 May 19 '16 at 19:50
0

My c++ function API.uart_read() was trying to put 64 bytes in buff and it was stepping on _ret.

I think. I know that is how you would describe it in C. When I made buff big enough to hold all 64 bytes things worked as expected.

lusher00
  • 678
  • 1
  • 7
  • 18