Skip to content

"SystemAcesssViolation" when generating offscreeen bitmaps #755

Open
@pathw0rk3r

Description

@pathw0rk3r

This is a follow on from my question #688. I'm happy to report that we have been successful in generating the GRContexts for both UWP and iOS and with it can create surfaces to generate bitmaps which get supplied as a serve to SKXamlCanvas or SKCanvasView controls. I have added comments in #688 about further details to help anyone which is interested in doing the same thing.

The app using the technology generates complex maps, often with thousands of features in a single bitmap. The features are built up in layers and come in as tiles. As tiles arrive, more layers are added to the bitmap, so the generation of the bitmap is like a daisy chain of progressive updates until all the layers have been added in.

Here is the central method:
image

So each time tiles arrives, a surface gets created, and its canvas is used to update the latest image through the renderActivity method. The latest bitmap then gets sent to the control for rendering. The calls to the Render method happen in a single thread and are sequential. When the control is heavily used, for instance to when the user is doing a series of quick pans and zooms, occasionally a "SystemAccessViolation exception occurs - 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'

Adding in the 'surface.Dispose()' call at the end of the method significantly reduces the number of times this occurs.

It seems that something is happening in the snapshotting and bitmap creation which doesn't close things properly.

I also noticed that combining the results of surfaces is most commonly the situation where the exception arises. I keep a bitmap of the features as they get built up and another one for markups. The tiles and the markup information come in at random times, but they are always processed sequentially in the method above. So a features bitmap is being generated and a markup bitmap is being generated. At the end I merge the two bitmaps together. The requests to the method come in via a queue, so I have some sense of what is going on when the exception happens. If there is the last update to the feature bitmap, followed by a quite update to the markups layers(which happens very quickly with its bitmap being mostly empty), it is during the merge processing of just drawing the features bitmap, followed by the markups bitmap that the exception arises. It is like the markups bitmap hasn't closed yet, and I'm trying to render it to a canvas. Is there any way to test if a bitmap is finished? Originally I passed back images to the control instead of bitmaps, but that caused these SystemAccessViolation messages almost continually. Here is the method that merges the bitmaps:

image

I wonder if it has to do with resources? I create the surfaces simply, without using the constructor that uses budgeting, or sample count. The documentation doesn't really explain what these parameters do or how they should be set. Could it be that the current settings are reusing a buffer somewhere which causes the conflict? I do everything in usings, so I thought this wouldn't be a problem. I also thought that the flush method would finish guarantee that the GPU has all the instructions it needs to generate the updates to the canvas.

In the interim, is there someway to be able to recover from the SystemAcessViolation exception? I'd really prefer not to have to put timers in things to slow everything down. The whole point was to make things go fast using the GPU (and when it works it goes really fast :-))

Any insights would be greatly appreciated,
Tom Cuthill

VS bug #770228

Metadata

Metadata

Assignees

Type

No type

Projects

Status

New

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions