Can the garbage collector reach the “garbage” left by a function written in another language?

Imagine that you are invoking a function from another language from a language with GC (for example, Fortran 95). The Fortran function leaves something allocated in memory between calls that may be visible in the caller’s language as unaccounted for garbage.

Can the GC from the caller’s language access the memory allocated in Fortran and consider it garbage and free it?

I guess this will not happen. The memory allocated by the Fortran function must have its own memory management, separate from the memory managed by the GC, however, I would be glad if anyone could confirm this.

Why do I need it? (if anyone is interested)

As described above, I need to write a function in F95 that allocates its own memory, is called several times and needs to keep a reference to the allocated memory between calls. The problem is that Fortran pointers are incompatible with the outside world, so I can’t just pass something like "void *" from Fortran. Therefore, the Fortran function will not save the pointer as a pointer, but throw it (for example) as an integer array for the outside world. However, if the GC might somehow interfere with Fortran memory, it might not realize that the link is stored in an integer array and might want to free the memory allocated in Fortran, which would be bad.

+3
source share
2 answers

, langauge ( ). .NET... ++ ++/CLI .NET , , , . .

++... - , .

+4

, , . , , - / , . Fortran () , , , .

Not knowing exactly what the garbage collector you are talking about is probably impossible to say with certainty.

0
source

Source: https://habr.com/ru/post/1735941/


All Articles