Quoth's picture

Pointer to vertex arrays get unpinned to early

Project:The Open Toolkit library
Version:1.0.0-rc1
Component:Documentation
Category:bug report
Priority:normal
Assigned:Unassigned
Status:won't fix
Description

Hi!
I was just browsing to the code and stumbled over something.

Affected functions:
TexCoordPointer
ColorPointer
IndexPointer
NormalPointer
VertexPointer

The pointer gets pinned (GCHandle.Alloc), passed to GL and unpinned (ptr.Free()). But this pointer will be stored within GL's state until its used (see OpenGL Specification 1.1, chapter 2.8).

By unpinning within the *Pointer method the garbage collector is free to move the array around or even free it. We end up with undefined behaviour. Its perfectly OK (from the specification point of view) to set the pointer once and let it there unchanged for a long time.

The correct moment to unpin it would be when the pointer within GL state is changed, for example by calling the *Pointer again. Note that there are other methods that do this implicitly.

I think would be diffcult for the wrapper to keep track of this, so I'd suggest to delegate it to the user. The correct pattern would then be that the user pins and unpins manually before passing the pointer to OpenTK.

Currently nothing about this problem is mentioned in the OpenTK documentation.


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

#1

Component:Code» Documentation
Priority:minor» normal
Status:open» confirmed

This is a documentation issue. Functions that use client-side storage (e.g. vertex arrays) are dangerous in garbage-collected environments like .Net - there's little OpenTK can do about this.

The best solution is to use server-side storage instead (e.g. vertex buffer objects).

Quoth's picture

#2

Agreed.
I've seen that OpenTK offers lots of overloads that allow passing managed arrays and similar. If the user must do the pinning, this overloads are of no use. They are actually quite missleading.

I'd suggest marking all but those taking an IntPtr as [Obsolete]. This should give OpenTK users a hint that they have a bug in their code, without breaking compatibility.

Inertia's picture

#3

Status:confirmed» won't fix

Information how to handle client-side Vertex Arrays can be found in the documentation. I don't think it's viable to document every possible pitfall with inline documentation. If we would go that road, we would have to add to every single gl function info like "you must query whether extension X and Y are supported", "it is an error to use this inside a begin-end block" and so on ...

the Fiddler's picture

#4

Agreed.