CF address book memory issues

View: New views
2 Messages — Rating Filter:   Alert me  

CF address book memory issues

by marcus_wood :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi there,

I've been developing an application which needs to access the address  
book. The problem is I don't think Im properly releasing my CF objects  
and so I subsequently get old data

There is an abstracted piece of code to demonstrate my problem below,  
but basically it prints the first name in a loop. The problem being  
that when you update the first name in address book it still prints  
out the old name, leading me to think that the old memory isn't being  
released

Any help would be really appreciated, :)

Thanks,
Marcus


PS: if you are trying the code out and need an address book UID if you  
export a vCard and open it with a text editor you can find it in there


#include <CoreFoundation/CoreFoundation.h>
#include <AddressBook/ABAddressBookC.h>

#include <stdio.h>
#include <unistd.h>

int GetProperty(char *UIDStr, CFStringRef propertyConstant, char  
*retVal, uint retValSize)
{
        CFStringRef UID;
        CFStringRef property;
        ABAddressBookRef addrBook;
        ABRecordRef record;
       
       
        UID = CFStringCreateWithCString(kCFAllocatorDefault, UIDStr,  
kCFStringEncodingMacRoman);
               
        addrBook = ABGetSharedAddressBook();
       
        record = ABCopyRecordForUniqueId(addrBook, UID);
               
        property = ABRecordCopyValue(record, propertyConstant);
               
        CFStringGetCString(property, retVal, retValSize,  
kCFStringEncodingMacRoman);

       
        CFRelease(property);
        CFRelease(record);
        CFRelease(UID);
       
        return (TRUE);
}
               

int main (int argc, const char * argv[])
{
        char propertyStr[255];
       
        while(1)
        {
                GetProperty("14FA361C-17D4-48DE-808C-5DD10A9299CA:ABPerson",  
kABFirstNameProperty, propertyStr, sizeof(propertyStr));
                printf("propertyStr: %s\n", propertyStr);
                sleep(2);
        }
       
     return 0;
}
_______________________________________________
MacOSX-dev mailing list
MacOSX-dev@...
http://www.omnigroup.com/mailman/listinfo/macosx-dev

Re: CF address book memory issues

by Christiaan Hofman-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I think you can be pretty sure this is a memory issue, rather a  
synchronization issue. An app using the addressbook framework needs to  
invoke ABSave before other apps will see any difference. It is not  
guaranteed that Address Book does this whenever you modify a name in  
it UI.

Christiaan

On Nov 1, 2009, at 17:28, Marcus Wood wrote:

> Hi there,
>
> I've been developing an application which needs to access the  
> address book. The problem is I don't think Im properly releasing my  
> CF objects and so I subsequently get old data
>
> There is an abstracted piece of code to demonstrate my problem  
> below, but basically it prints the first name in a loop. The problem  
> being that when you update the first name in address book it still  
> prints out the old name, leading me to think that the old memory  
> isn't being released
>
> Any help would be really appreciated, :)
>
> Thanks,
> Marcus
>
>
> PS: if you are trying the code out and need an address book UID if  
> you export a vCard and open it with a text editor you can find it in  
> there
>
>
> #include <CoreFoundation/CoreFoundation.h>
> #include <AddressBook/ABAddressBookC.h>
>
> #include <stdio.h>
> #include <unistd.h>
>
> int GetProperty(char *UIDStr, CFStringRef propertyConstant, char  
> *retVal, uint retValSize)
> {
> CFStringRef UID;
> CFStringRef property;
> ABAddressBookRef addrBook;
> ABRecordRef record;
>
>
> UID = CFStringCreateWithCString(kCFAllocatorDefault, UIDStr,  
> kCFStringEncodingMacRoman);
>
> addrBook = ABGetSharedAddressBook();
>
> record = ABCopyRecordForUniqueId(addrBook, UID);
>
> property = ABRecordCopyValue(record, propertyConstant);
>
> CFStringGetCString(property, retVal, retValSize,  
> kCFStringEncodingMacRoman);
>
>
> CFRelease(property);
> CFRelease(record);
> CFRelease(UID);
>
> return (TRUE);
> }
>
>
> int main (int argc, const char * argv[])
> {
> char propertyStr[255];
>
> while(1)
> {
> GetProperty("14FA361C-17D4-48DE-808C-5DD10A9299CA:ABPerson",  
> kABFirstNameProperty, propertyStr, sizeof(propertyStr));
> printf("propertyStr: %s\n", propertyStr);
> sleep(2);
> }
>
>    return 0;
> }
> _______________________________________________
> MacOSX-dev mailing list
> MacOSX-dev@...
> http://www.omnigroup.com/mailman/listinfo/macosx-dev

_______________________________________________
MacOSX-dev mailing list
MacOSX-dev@...
http://www.omnigroup.com/mailman/listinfo/macosx-dev