mapbox / mapbox-scenekit

Other
231 stars 51 forks source link

Crash in MBImageAPI #34

Open natalia-osa opened 6 years ago

natalia-osa commented 6 years ago

Crash happens when trying to fetch a larger map size.

MapboxImageAPI.swift line 227:

group.notify(queue: DispatchQueue.main) {
    self.pendingFetches.removeValue(forKey: groupID)
    completion(!hadFailure ? imageBuilder.makeImage() : nil) // HERE: EXC_BAD_ACCESS (code=2
}

what later affects:

terrainNode.fetchTerrainTexture("mapbox/satellite-v9", zoom: 14, progress: { _, _ in }, completion: { image in
    terrainNode.geometry?.materials[4].diffuse.contents = image // here
})

In the console I receive tons of following informations: 2018-08-28 13:46:51.938021+0200 AppName[1937:978808] [Unknown process name] CGBitmapContextInfoCreate: unable to allocate 608605984 bytes for bitmap data

avi-c commented 6 years ago

This issue and Issue #33 have the same root cause. Since the SDK right now uses a fixed zoom level of 12 when asking for tiles regardless of the lat/lon bounds that are being rendered, there are problems when the lat/lon bounds exceed some threshold. The image API will request the necessary number of tiles at zoom level 12 that are needed to cover the request bounds. When it tries to stitch all those tiles together into a single CGImage for the terrain image or heightmap image, if the resultant CGImage would exceed the allowable size then no image is created and we'll later crash because of it.

I have an experimental fix for this that dynamically calculates the zoom level while trying to minimize the size of the final image. I don't think it's quite ready to merge into master, but I'll put it up as a PR for everyone to check out.

avi-c commented 6 years ago

I created PR #39 with my proposal for preventing this crash.