Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There should be implemented a WeakPtr also #11

Open
Q-Master opened this issue Nov 22, 2021 · 6 comments
Open

There should be implemented a WeakPtr also #11

Q-Master opened this issue Nov 22, 2021 · 6 comments

Comments

@Q-Master
Copy link

Q-Master commented Nov 22, 2021

This code will leak because a refers to b and b refers to a and there's no possibility to fix this not using WeakPtr.

import threading/smartptrs

type
  Widget = object
    oth: SharedPtr[Widget]


var a: SharedPtr[Widget] = newSharedPtr(Widget())
var b: SharedPtr[Widget] = newSharedPtr(Widget())
a[].oth = b
b[].oth = a
echo a
echo b

Also this code goes to infinite recursion in echos.

@Araq
Copy link
Member

Araq commented Nov 22, 2021

You can use a ptr though for the "weak" pointer.

@Q-Master
Copy link
Author

Yes I can, but also I can use ref instead of SmartPtr.
Might it be better just implement some sort of "unique" keyword in Nim itself instead of implementing a new batch of pointers copied from C++ and having the same problems which do they have in C++?
Orelse you should say "B" when spoken "A" and implement a WeakPtr too.

@Araq
Copy link
Member

Araq commented Nov 25, 2021

I don't understand your point. Adding "unique" to Nim's type system would be much more work than a library solution, obviously.

@Q-Master
Copy link
Author

The point is that unique keyword in the Nim itself will add possibility to use any Nim supported type, not only pointers and make this library redundant.

Also this lib lacks WeakPtr, which is obviously should be there. WeakPtr is not just pointer, it is a smart pointer too: it has use count, expiration, destructor and assignment ops and could be locked (converted atomically to SharedPtr).

@Araq
Copy link
Member

Araq commented Dec 1, 2021

So add a WeakPtr, you don't have to lecture me what it is. My point was that the existing SharedPtr is far from useless as it is now and that many designs work with ptr as the "unowned" pointer type.

@Q-Master
Copy link
Author

Q-Master commented Dec 1, 2021

I'm just talking that adding unique kw to Nim itself will be better than porting some OOP solutions from C++ with all it's bugs and vulnerabilities (e.x. SharedPtr is unsafe in threading) to the language with very limited OOP.
Using ptr as unowned pointer is unsafe in this solution because WeakPtr is not a pointer itself and not providing any dot operations to underlying pointer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants