Detect memory violations

Luna enables dynamic use of memory objects in RAM. The memory objects in memory are strings and memory blocks.

See also: Memory Management

In order to see whether data of the active memory objects are not damaged, an appropriate monitoring functionality via exceptions is implemented in Luna. By means of such monitoring may mistake as buffer overflows, accesses via object boundaries, and lack of memory are detected. See also: exceptions With the exceptions OutOfBoundsMemory and NilObjectException accesses via memory limits or not generated memory objects are detected.

Example

We generate a memory block and write data into it. If now tries outside the memory block to be written by a programming error in the data memory, this triggers the exception OutOfBoundsMemory. Since omitted for performance reasons without implementation of the exceptions, the monitoring functions, this can lead to damage to the internal memory structure and thus to be cascading serious program errors. The exception allows here to find such memory violations.

const F_CPU=20000000
avr.device = atmega328p
avr.clock  = F_CPU
avr.stack = 32
 
uart.baud = 19200
uart.recv.enable
uart.send.enable
 
dim i as byte
dim m as MemoryBlock
 
print 12;"out of bounds exception example"
print
'Memory block to allocate 16 bytes of memory
m = New MemoryBlock(16)
'Check whether the memory block could be allocated
if m<>nil then
  'Write to the memory via a loop. Outside the
  'Storage capacity of the allocated memory block is the
  'exception thrown.
  for i=0 to 20
    print "schreibe byte ";str(i);
    m.ByteValue(i)=i
    print " - ok"
  next
else
  print "Error: memory block could not be allocated "
end if
do
loop
exception OutOfBoundsMemory
  ' Output error message
  Print "*** exception OutOfBoundsMemory ***"
endexception