The solution is to make everyone only write code in languages which use a runtime that controls memory access better than the OS can. Python or Java, for example. Failing that, relying on the OS is the safest way, and the simplest, most secure way to do that is to keep GPG in its own process.
I mentioned two: Python and Java.
In general, I mean any language where the runtime only hands programs valid pointers and the language doesn't allow pointer arithmetic. This means it's possible to guarantee that pointers will only point into objects that can be validly accessed by the code which holds those pointers.
This is generally done to make garbage collection easier, but it can be done to implement security policy: Basically, if your security policy trusts a given runtime, you can use that runtime to enforce access policies on objects of all kinds, including memory.