Can I remove an element from a global associative array from within a class destructor?
realhet
real_het at hotmail.com
Fri Aug 2 23:13:10 UTC 2019
Hi,
I tried to make some resource statistict for my OpenGL Buffer
objects:
//here are the things that hold the statistics.
private __gshared{ size_t[int] textureSizeMap, bufferSizeMap; }
struct GLCounters{
int programs, shaders, textures, buffers;
size_t textureSize, bufferSize;
}
__gshared GLCounters glCounters;
...
//And this is the buffer creation.
int genBuffer(size_t size){
int res; glGenBuffers (1, &res); glChk;
glCounters.buffers ++;
glCounters.bufferSize += size;
bufferSizeMap [res] = size;
writefln("buffer allocated %d %d", res, size);
return res;
}
//Finally, this is the deallocation. If it is called from the GC
(when it destroys a class), it has a big chance to throw an
Invalid Memory Operation exception.
void deleteBuffer (int handle){
if(!handle) return;
glDeleteBuffers (1, &handle); glChk;
glCounters.buffers --;
glCounters.bufferSize -= bufferSizeMap [handle];
writefln("buffer deallocated %d %d", handle, bufferSizeMap
[handle]);
bufferSizeMap.remove(handle); <- this is the problematic part.
}
--------------------------------------------
Today I read the documentation about structs, unions and classes,
but I haven't find any restrictions for the ~this() destructors.
Is there some extra rules regarding the GC and what I must not do
in the destructors?
I think the destructor always called in the same thread where the
instance was created. This can't be the case.
But what I can guess is: The GC makes a collection and calls my
destructor and inside I do something and the GC calls a
collection again recursively. But it seems a bit crazy, so I
think it's not the case :D
Please help me solve this!
*I'm using LDC 1.6.0 Win64
More information about the Digitalmars-d-learn
mailing list