Unity freezes when calling terrainData.SetHeights

I am trying to use Mathf.perlinnoise to randomly create terrain for my game, but whenever I call terrainData.SetHeights unity just freezes and stops responding. I looked around but didn’t find anyone else encountering this problem. Is there something obvious I am overlooking in my code? I am using unity 4.0.0f7

	void generateMap(int seed, int perlinwidthperpixel) {
		Random.seed = seed;
 		float[,] heights = new float[(int)terrain.terrainData.heightmapWidth, (int)terrain.terrainData.heightmapHeight];
		for (int i = 0 ; i < terrain.terrainData.heightmapWidth ; i++) {
			for (int k = 0 ; k < terrain.terrainData.heightmapHeight ; k++) {
				float perlin = Mathf.PerlinNoise(i*perlinwidthperpixel,k*perlinwidthperpixel);
				heights[i,k] = perlin;

SetHeights for larger terrains is very slow and generating the Perlin noise will also take a bit of processing. Most likely it is not crashed but simply executing the function. Wait a while longer and see if it finishes or experiment with a terrain with a very small resolution, such as 128x128.

You could stick it into a coroutine if you don’t want the game to freeze while executing the method.

I managed to fix the problem partially.
If I use terrain.terrainData.heightmapHeight-1 instead of terrain.terrainData.heightmapHeight in my code everything works just fine. This does cause the last vertices of my terrain to not by affected by the generation, which is a problem.
Is this a bug in unity?