digitalmars.D.learn - Garbage Collector : Ignoring a reference
- Begah (25/25) Apr 26 2016 I am trying to create an asset manager for my textures. I had the
- Alex Parrill (3/9) Apr 26 2016 What you want are "weak references". I don't think D supports
- ciechowoj (3/6) Apr 26 2016 You could always zero the reference in the hashmap, as it won't
- Begah (13/19) Apr 26 2016 Nothing will reload.
- ag0aep6g (2/11) Apr 26 2016 How would you prevent reads of that now-invalid element of the hashmap?
- ciechowoj (4/6) Apr 26 2016 So, having pointer that doesn't hold a reference isn't that hard
- Marco Leise (30/34) Apr 26 2016 My ideological point of view is that you must not use
- Namespace (9/34) Apr 30 2016 Texture[string] textures;
- Guillaume Piolat (7/32) Apr 30 2016 You should avoid to rely on the garbage collector to free
I am trying to create an asset manager for my textures. I had the idea ( it may be a wrong idea ) to create a hashmap of my textures with a string as the key. When the program request a texture, it firts check if it is in the hashmap and then returns if it is : Texture[string] textures; Texture loadTexture(string filename) { if(filename in textures) return textures[filename] else // Load image and put it in hashmap } Warning : I haven't tested if it actually doesn't work, but thinking about it, i think it should not. My problem is that i return a reference of the texture to the program, but i keep one to myself and i want to free the texture if my program isn't using it anymore ( no more reference to it ). The problem i see, is that i will always have atleast one reference to the texture in my hashmap, but i want the garbage collector to ignore that reference and to free the texture if there are no more references anywhere in my program ( except in the hashmap ). How could i tell the garbage collector to ignore the reference in the hashmap and to free it if there isn't any other reference that in my hashmap?
Apr 26 2016
On Tuesday, 26 April 2016 at 09:07:59 UTC, Begah wrote:I am trying to create an asset manager for my textures. I had the idea ( it may be a wrong idea ) to create a hashmap of my textures with a string as the key. When the program request a texture, it firts check if it is in the hashmap and then returns if it is : [...]What you want are "weak references". I don't think D supports them yet.
Apr 26 2016
On Tuesday, 26 April 2016 at 09:07:59 UTC, Begah wrote:How could i tell the garbage collector to ignore the reference in the hashmap and to free it if there isn't any other reference that in my hashmap?You could always zero the reference in the hashmap, as it won't be valid after reload anyway...
Apr 26 2016
On Tuesday, 26 April 2016 at 13:01:26 UTC, ciechowoj wrote:On Tuesday, 26 April 2016 at 09:07:59 UTC, Begah wrote:Nothing will reload. An example : I load a texture "button.png" in a class and draw it to the screen, When the screen switches to another screen ie from menu to the game, I want that the "button.png" texture is automaticly destroyed by the gc. But this will never happen because i still have a reference to it in my hashmap. Thus, i need a way to tell the gc to ignore the reference ( or something similar ) in that hashmap.How could i tell the garbage collector to ignore the reference in the hashmap and to free it if there isn't any other reference that in my hashmap?You could always zero the reference in the hashmap, as it won't be valid after reload anyway...
Apr 26 2016
On 26.04.2016 15:35, Begah wrote:Nothing will reload. An example : I load a texture "button.png" in a class and draw it to the screen, When the screen switches to another screen ie from menu to the game, I want that the "button.png" texture is automaticly destroyed by the gc. But this will never happen because i still have a reference to it in my hashmap. Thus, i need a way to tell the gc to ignore the reference ( or something similar ) in that hashmap.How would you prevent reads of that now-invalid element of the hashmap?
Apr 26 2016
Thus, i need a way to tell the gc to ignore the reference ( or something similar ) in that hashmap.So, having pointer that doesn't hold a reference isn't that hard (store it in memory region that is unreachable to gc), but don't you need a way to tell if that pointer ins't dangling, beyond initial problem?
Apr 26 2016
Am Tue, 26 Apr 2016 13:35:37 +0000 schrieb Begah <mathieu.roux222 gmail.com>:When the screen switches to another screen ie from menu to the game, I want that the "button.png" texture is automaticly destroyed by the gc.My ideological point of view is that you must not use non-deterministic garbage collection for resources. Neither for files, GUI widgets or textures. The garbage collector cannot look past its own confined environment (heap memory allocated by the D program and loaded D libraries) and will not have enough information to tell if the process is running out of file handles, backing buffer for widgets or texture memory on the graphics card. It only takes action, when its own heap is filling up or when you manually call GC.collect(). All the convenient 100% GC languages from Java to Python require the user to call ".close()" for files, ".dispose()/.Destroy()" for widgets, etc. Reference counting is the correct approach. It makes using external resources safe and convenient by removing the need for manual lifetime management. Cyclic references cannot exist in the D code in this scenario. The texture constructor: - sets ref count to 1 - adds texture to hash map The copy constructor: - increments the ref count The destructor: - decrements the ref count - if ref count reaches 0: - removes the texture from the hash table - destroys the texture data -- Marco
Apr 26 2016
On Tuesday, 26 April 2016 at 09:07:59 UTC, Begah wrote:I am trying to create an asset manager for my textures. I had the idea ( it may be a wrong idea ) to create a hashmap of my textures with a string as the key. When the program request a texture, it firts check if it is in the hashmap and then returns if it is : Texture[string] textures; Texture loadTexture(string filename) { if(filename in textures) return textures[filename] else // Load image and put it in hashmap } Warning : I haven't tested if it actually doesn't work, but thinking about it, i think it should not. My problem is that i return a reference of the texture to the program, but i keep one to myself and i want to free the texture if my program isn't using it anymore ( no more reference to it ). The problem i see, is that i will always have atleast one reference to the texture in my hashmap, but i want the garbage collector to ignore that reference and to free the texture if there are no more references anywhere in my program ( except in the hashmap ). How could i tell the garbage collector to ignore the reference in the hashmap and to free it if there isn't any other reference that in my hashmap?Texture[string] textures; Texture* loadTexture(string filename) { if(filename in textures) return &textures[filename] else // Load image and put it in hashmap } Texture* tex = loadTexture(...);
Apr 30 2016
On Tuesday, 26 April 2016 at 09:07:59 UTC, Begah wrote:I am trying to create an asset manager for my textures. I had the idea ( it may be a wrong idea ) to create a hashmap of my textures with a string as the key. When the program request a texture, it firts check if it is in the hashmap and then returns if it is : Texture[string] textures; Texture loadTexture(string filename) { if(filename in textures) return textures[filename] else // Load image and put it in hashmap } Warning : I haven't tested if it actually doesn't work, but thinking about it, i think it should not. My problem is that i return a reference of the texture to the program, but i keep one to myself and i want to free the texture if my program isn't using it anymore ( no more reference to it ). The problem i see, is that i will always have atleast one reference to the texture in my hashmap, but i want the garbage collector to ignore that reference and to free the texture if there are no more references anywhere in my program ( except in the hashmap ). How could i tell the garbage collector to ignore the reference in the hashmap and to free it if there isn't any other reference that in my hashmap?You should avoid to rely on the garbage collector to free non-memory resources anyway. This is accidental correctness. You are not guaranteed to get called by the GC, or by the appropriate thread. You can call .destroy() on these textures when in the texture manager destructor.
Apr 30 2016