11-Sep-2013 17:33, Joseph Rushton Wakeling пишет:
On 11/09/13 15:13, monarch_dodra wrote:
That's somewhat better, as it would allow the GC to collect my buffer,
if it
wants to, but I wouldn't actually know about it afterwards which
leaves me screwed.
Just to clarify, is this buffer meant only for internal use in your
function or is it meant to be externally accessed as well? I'd kind of
assumed the former.
Either way, isn't it sufficient to have some kind of
if (buf is null)
{
// allocate the buffer
}
check in place? The basic model seems right -- at the moment when you
need the buffer, you check if it's allocated (and if not, allocate it as
needed); you indicate to the GC that it shouldn't collect the memory;
you use the buffer; and the moment it's no longer needed, you indicate
to the GC that it's collectable again.
It means having to be very careful to check the buffer's allocation
status whenever you want to use it, but I think that's an unavoidable
consequence of wanting a static variable that can be freed if needed.
Problem is - said GC-freed memory could be then reused in some way. I
can't imagine how you'd test that the block that is allocated is *still
your old* block.
--
Dmitry Olshansky