Can someone provide a simple/very simple reference counting or refcounted example i can understand. Thanks.
Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
April 26, 2022 Reference counting example | ||||
---|---|---|---|---|
| ||||
April 26, 2022 Re: Reference counting example | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alain De Vos | On Tuesday, 26 April 2022 at 06:55:34 UTC, Alain De Vos wrote: >Can someone provide a simple/very simple reference counting or refcounted example i can understand. Thanks. I suggest to look at RefCounted here rather than in Phobos. There are simple examples. |
April 26, 2022 Re: Reference counting example | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alain De Vos | On Tuesday, 26 April 2022 at 06:55:34 UTC, Alain De Vos wrote: >Can someone provide a simple/very simple reference counting or refcounted example i can understand. Thanks. I've been playing around with the automem[1] library's RefCounted feature as we speak, it seems to fit my needs more than std.typecons which doesn't quite do what I want. I did have to make some changes to the library though to allow for inheritance and manually releasing (below). It's pretty fun so far so I'm looking forward to trying it in some other projects like a non-GC XML library. Test application:
Output:
I added the following functions to automem
|
April 26, 2022 Re: Reference counting example | ||||
---|---|---|---|---|
| ||||
Posted in reply to cc | On Tuesday, 26 April 2022 at 22:16:01 UTC, cc wrote: >Test application: I should point out that all this stuff with saving refcounted things to arrays and so on is extremely untested and experimental🙄 One problem I'm seeing is the inability for a refcounted class to pass itself to another function, since the class internals don't see the struct wrapper.. you can pass the naked object reference itself, and hope the reference doesn't get saved otherwise there's your dangling pointer, but then you also have the problem of inconsistent method declarations, with some things taking Foo and others taking RefCounted!Foo etc... Every night I pray for a |
April 27, 2022 Re: Reference counting example | ||||
---|---|---|---|---|
| ||||
Posted in reply to cc | On Tuesday, 26 April 2022 at 23:33:28 UTC, cc wrote: >On Tuesday, 26 April 2022 at 22:16:01 UTC, cc wrote: >Test application: I should point out that all this stuff with saving refcounted things to arrays and so on is extremely untested and experimental🙄 One problem I'm seeing is the inability for a refcounted class to pass itself to another function, since the class internals don't see the struct wrapper.. you can pass the naked object reference itself, and hope the reference doesn't get saved otherwise there's your dangling pointer, but then you also have the problem of inconsistent method declarations, with some things taking Foo and others taking RefCounted!Foo etc... Every night I pray for a Yor code has antoher big problem. Class Animal has @safe/pure/nothrow/@nogc destruction but class Cow has @system destructor. When you assign RC!Cow to RC!Animal you can have @safe @nogc ... function call @system destructor of Cow. I have library with check this kind of error for you (https://code.dlang.org/packages/btl). If you need aliasing (your case) or weak pointers then try it.
|