Good catch on the resize() function. That will help with this bug. I would recommend using resize() if you know (approximately) how large your mesh will be — it’s better than having the mesh re-allocate whenever it gets too large.

I am still working through how `myMesh:resize()`

works, by reference to the Codea Runtime Library. Below is an example of the use of my solution. `s`

has to be a multiple of 3000 that is an integral power of 2, so that capacity is only increased one more time, when the first rectangle is added to the mesh. (Update) Updated for my better understanding of `myMesh:resize()`

.

```
-- Version 2012.06.30.15.00
function setup()
local n = 40 -- Size of edge of a grid of squares
local l = math.min(WIDTH, HEIGHT) / n -- Length of each square in grid
myBigMesh = mesh()
local myBigMeshMaxSize = n * n * 6
-- It is important to use a multiple of 3000 that is an
-- integral power of 2 (and a multiple of 6)
local s = 3000 * 2^math.ceil(math.log(myBigMeshMaxSize/3000)/math.log(2))
myBigMesh:resize(s)
for j = 1, n do
for i = 1, n do
-- Width and height one pixel short of l, to create borders in grid:
local idx = myBigMesh:addRect((i-1)*l + l/2, (j-1)*l + l/2, l-1, l-1)
-- Pick a random colour:
local c = color(math.random(255), math.random(255), math.random(255))
myBigMesh:setRectColor(idx, c)
end
end
end
function draw()
background(0)
myBigMesh:draw()
end
```

I updated my comment above, for my better understanding of `myMesh:resize(size)`

:

If size is a positive integer, Lresize calls resizeBuffer with newLength equal to size.

The buffer has a capacity and a length (of ‘GLfloat’ units of elementSize - being 3 for the vertices). (The initial capacity is 3000. The inital length is 0.)

resizeBuffer always, finally, sets the length to newLength.

If newLength is greater than capacity, then capacity is repeatedly doubled until it is bigger or equal to newLength. Memory is then reallocated, based on the enlarged capacity. Then there is some (partial?) zero-ing out of the buffer (which is indexed from 0) between the original length (bug: should be length + 1, otherwise the end of the previous content is zero-ed) and newLength:

`buffer->buffer[ i * buffer->elementSize ] = 0`