-
Notifications
You must be signed in to change notification settings - Fork 421
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
Fix deletion of Array elements #172
Conversation
I don't think you want to do this. Because these are objects, and not arrays, I'm lead to believe that you aren't using |
Yes, if we don't want to do this, then I think we should just check for an I could send a pull request with it if that's required to be done. |
Maybe if you included what you were doing it would be easier to figure out the right thing to do. I'm assuming you are iterating one of these objects, else you would know if something existed or not. I would make my loop look something like: for (var chan in channels) {
if (channel.hasOwnProperty(chan)) {
<do stuff>
}
} |
I suspect this is the cause of #171, due to array delete mechanics not being a true delete; if we wanted a proper delete, then there's always the Array.prototype.remove extension that John Resig wrote. http://stackoverflow.com/a/9815010/783103 && http://ejohn.org/blog/javascript-array-remove/ ed: Yeah, this is the problem. node-irc's builtin tracking of users-in-channel and such is the culprit of those crashes, and this would be the solution. Also, @jirwin's concern completely misses the mark here imo. These are arrays, not objects, as delete's behavior is leaving behind |
Yes, its also because of the same reason. This pull request fixes this by using the similar solution as given in that Stack Overflow link. |
It may be worthwhile to write a tiny wrapper function around splice to indicate that it's for deleting an array entry if Resig's solution isn't used; using the splice as-is is a little harder to read and grok right away. |
@damianb Lets look at what objects this PR interacts with, and where those are defined in the code.
Unless I'm missing something completely, these are all Objects and not arrays. It looks like https://github.com/sankha93/node-irc/blob/35fe7e9426cadf893e5f05fe27a0d665bfe11d28/lib/irc.js#L276 is iterating |
Just blindly using hasOwnProperty isn't a proper way to handle it though. If there's undefined just sitting inside the object/array/whathaveyou, that's allocated memory that should be freed back up. |
@damianb I really don't believe that to be the case. Look at this snippet from the node REPL: node
> var foo = {baz: 'qux'};
undefined
> foo
{ baz: 'qux' }
> foo['baz'];
'qux'
> foo['monkey'];
undefined
> delete foo['baz'];
true
> foo['baz'];
undefined
> foo
{} Javascript returns |
There is a reason that property is still being iterated over; check |
@damianb There is a difference between an Object property with an undefined value, and a property that isn't defined on the Object. To use your own example: node
> var foo = {baz: 'qux'};
undefined
> foo
{ baz: 'qux' }
> foo['baz'];
'qux'
> Object.keys(foo);
[ 'baz' ]
> foo['monkey'];
undefined
> delete foo['baz'];
true
> foo['baz'];
undefined
> foo
{}
> Object.keys(foo);
[] |
...Yes, and the problem here that is causing crashes is _an entry or property that has an undefined value_ that the code is attempting to iterate on and interact with. |
So did we reach any unanimous decision over here? I would still like to avoid |
Okay, after reading all the comments, I'm inclined to think this pull request is the wrong solution. Clearly the references in question are I'm going to close this pull request. If someone wants to make a new one that fixes the loop above, I'd happily merge that :-) |
Node-IRC uses
delete
keyword to remove things from Array which actually sets it toundefined
. This causes unexpected errors like:This patch fixes this problem.