views:

56

answers:

2

Imagine that you call from a language with GC repetitively a function from another language (e.g., Fortran 95). The Fortran function leaves something allocated in the memory between calls which might be seen from the caller language as unreferenced rubbish.

Could GC from the caller language access the memory allocated in Fortran and consider it as rubish and free it?

I guess that it won't happen. The memory allocated by the Fortran function should have its own memory management separated from the memory managed by GC, however, I would be happy if anyone could confirm that.

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

As described above, I need to write a fucntion in F95 which allocates its own memory, is called several times and it needs to keep the reference to the allocated memory between calls. The problem is that Fortran pointers are incompatibile with the outside world so I can't just pass something as 'void *' from Fortran. Therefore the Fortran function would store the pointer not as a pointer but would cast it (for example) as an integer array for the outside world. However, if GC could anyhow interfere memory from Fortran, it might not understand that the reference is kept in the integer array and might want to free the memory allocated in Fortran, which would be bad.

Thanks for your time. Jan

+3  A: 

No, unless the language explicitely integrated with the host langauge (using the garbage collector). In .NET... a C++ application can use C++/CLI to allocate .NET objects and return those - and those naturally are garbage collected. I do that in a number of projects.

But a pure C++ object... the garbage colelctor knows nothing about and does not know how to handle.

TomTom
A: 

There probably isn't a single answer to this question that's guaranteed to be correct. As a rule, however, a garbage collector will be associated with some sort of heap allocator, and can/will only collect memory within the heap it controls. Since your Fortran function will (presumably) allocate its memory completely separately, it probably won't be affected by the garbage collector.

Without knowing exactly what garbage collector you're talking about, it's probably not possible to say with certainty though.

Jerry Coffin