Thread overview
Trying to implement a reference counting mechanism
Mar 19, 2013
Martin
Mar 19, 2013
Brad Anderson
Mar 19, 2013
Martin
Mar 19, 2013
Martin
Mar 20, 2013
Mike Wey
Mar 20, 2013
Martin
March 19, 2013
http://pastebin.com/8M2EuyfT

I'm fairly new to D, but would this work? Obviously I would need to make my own kind of associative array that uses manual memory management to completely circumvent the GC, but besides that?
March 19, 2013
On Tuesday, 19 March 2013 at 22:06:31 UTC, Martin wrote:
> http://pastebin.com/8M2EuyfT
>
> I'm fairly new to D, but would this work? Obviously I would need to make my own kind of associative array that uses manual memory management to completely circumvent the GC, but besides that?

Have you seen std.typecons.RefCounted[1]?  If you are just looking to learn more so than get a RefCounted class you could take some idea's from its source code[2].

[1] http://dlang.org/phobos/std_typecons.html#.RefCounted
[2] https://github.com/D-Programming-Language/phobos/blob/master/std/typecons.d#L2644
March 19, 2013
I was inspired by the "Unmanaged" framework, and the usage would be something like:

class MyClass
{
private:
    int _a;
    int _b;

public:
    this(int a, int b)
    {
        _a = a;
        _b = b;
    }
}

class OtherClass
{
private:
    RefCountedObj!MyClass _myClassInstance;

public:
    this()
    {
        _myClassInstance = RefCountedObj!MyClass.create(10, 20);
    }
}
March 19, 2013
On Tuesday, 19 March 2013 at 22:09:22 UTC, Brad Anderson wrote:
> On Tuesday, 19 March 2013 at 22:06:31 UTC, Martin wrote:
>> http://pastebin.com/8M2EuyfT
>>
>> I'm fairly new to D, but would this work? Obviously I would need to make my own kind of associative array that uses manual memory management to completely circumvent the GC, but besides that?
>
> Have you seen std.typecons.RefCounted[1]?  If you are just looking to learn more so than get a RefCounted class you could take some idea's from its source code[2].
>
> [1] http://dlang.org/phobos/std_typecons.html#.RefCounted
> [2] https://github.com/D-Programming-Language/phobos/blob/master/std/typecons.d#L2644

Yes, I have indeed studied the RefCounted struct in std.typecons. Unfortunately it only works with structs, not classes. I just wanted to try a different approach to see if it would work
March 20, 2013
On 03/19/2013 11:06 PM, Martin wrote:
> http://pastebin.com/8M2EuyfT
>
> I'm fairly new to D, but would this work? Obviously I would need to make
> my own kind of associative array that uses manual memory management to
> completely circumvent the GC, but besides that?

It looks like your refCount is off by one, i think you need to set it to 1 in the refCountedObj function, or check for refCount < 0 before freeing the value.

-- 
Mike Wey
March 20, 2013
On Wednesday, 20 March 2013 at 19:19:09 UTC, Mike Wey wrote:
> On 03/19/2013 11:06 PM, Martin wrote:
>> http://pastebin.com/8M2EuyfT
>>
>> I'm fairly new to D, but would this work? Obviously I would need to make
>> my own kind of associative array that uses manual memory management to
>> completely circumvent the GC, but besides that?
>
> It looks like your refCount is off by one, i think you need to set it to 1 in the refCountedObj function, or check for refCount < 0 before freeing the value.

Oh, my bad, I actually had that in the code, but when I copy/pasted it on pastebin, I had to remove some comments n stuff and I accidentally removed it along with a "import std.conv".