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

Introduce duplication function for Objective-C objects #143

Closed
wants to merge 4 commits into from

Conversation

Gricha
Copy link

@Gricha Gricha commented Mar 1, 2016

This commit introduces very basic duplicate detection for Objective-C objects.

//

#if __has_feature(objc_arc)
#error CHLDuplicateDetection.m expects ARC to be disabled.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file doesn't need ARC disabled. And actually, it needs ARC enabled because there are no -release calls, so the objects allocated in here are leaking.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah you are right, in the previous version I used MRR intentionally but end-solution definitely does not need it. I'll clean it up

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I tested again, and I think it should be MRR after all. Reason for that is that object iterator will return anything that is an object, but cannot be retained. Example:

*** -[NSTaggedPointerStringCStringContainer retain] called, not supposed to happen

NSMutableArray *objects = [NSMutableArray new];

CHLEnumerateObjectsWithBlock(^(id object) {
if (object_getClass(object) == cls) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should use [object isKindOfClass:cls] here? It will be pretty common to look for duplicates of NSString, NSArray, NSDictionary, etc, but those are all implemented as class clusters and as such are essentially abstract classes and no instances will be found. These collection objects will actually be internal subclasses, and I we definitely don't want to force callers to refer to internal subclasses for many reasons.

On the flip side, there might be cases where the caller wants to find duplicates based on exact class matching, not subclasses. I suspect this will be the less common case, so I would be fine with not supporting this case right now, and dealing with it later.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with both. I think using isKindOfClass for now might actually be better, since we might want to look at subclasses right away. Let me change that.

return;
}

if (range.size > class_getInstanceSize(class_) && object_getIndexedIvars(object) == NULL) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might not work, it will have to be tested or double checked. My question is around the nature of the range values returned by the malloc system. Does range.size indicate the class instance size, or does it indicate the full size of the block of memory that malloc returned.

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

Successfully merging this pull request may close these issues.

3 participants