jeske's picture

why IntPtr instead of unsafe struct *?

I'm fishing around trying to get true precise mouse-wheel info to work on MacOS, and I'm surprised that OpenTK uses IntPtr instead of unsafe struct * to type foreign datatypes.

Any particular reason for this? It makes the code really hard to read, and also makes mistakes pretty easy. I know MSFT advocates IntPtr, though I have no idea why. It's not as if they are "safe".

Years ago I wrote an article on the technique of using "unsafe struct *", in-case this is unfamiliar to someone.

http://www.codeproject.com/Articles/339290/PInvoke-pointer-safety-Replac...


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
smiley80's picture
jeske wrote:

I know MSFT advocates IntPtr, though I have no idea why.

'Unsafe' pointers aren't CLS compliant and hence cannot be used in every CLI language (e.g. VB.Net).

Edit:
Ignore the above, I misread something.

kanato's picture

I'm not sure.. I don't think the idea of using unsafe struct * ever occurred to me when I was writing the MacOS bindings way back when. It seems like a nice idea for type safety though.

the Fiddler's picture

Mono used to have serious issues when marshaling structs back in the old days. OpenTK has been around since the Mono 1.x days, and it wasn't until the 2.x releases that these issues were fixed (2.6 and later were generally ok, but some issues remained until 2.12 or so, IIRC. Callback functions and structs were the biggest offenders.)

In any case, the choice between IntPtr and unsafe pointers in the internal platform APIs is not critical, runtime bugs notwithstanding.