doob wrote:
I thought if you had a type safe language you could write type safe memory management code.
Yes, if the type system is expressive enough, but for languages like Java, C#, and D, it's not.
Think of it like this -- type safety is really just being used a proxy for memory safety. What we really want to avoid at the end of the day is leaked memory, reading from or writing to memory that doesn't belong to our program, etc. All that type safety means is that the language enforces a certain kind of consistency -- any operation you can express must follow certain rules. Type systems in conventional GC'd languages are geared towards guaranteeing basic memory safety -- that is, if you follow the rules, your program is type-safe, and because the type system is designed properly, it is also memory-safe.
The problem with GCs and memory managers is that they do things to untyped memory all the time. There are relationships and characteristics of those memory regions manipulated by a GC or MM that are not possible to formalize using a conventional type system (as the OP has discovered).
Try writing a type-safe GC or memory manager and you'll see what the problem is, even if it's difficult to put into words.