Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
TemplateCFObject_AC
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

TemplateCFObject_AC



Hi All,

I have been using the classes CAutoCFURL_AC, CAutoCFBundle_AC which
inherit via templates from the class TemplateAutoRef_AC in CCFObject_AC.h

I have just realized that I have been using these auto classes
incorrectly and this results in memory leaks. I have fixed my code for
what I am currently working on but I am interested in why it is designed
the way that it is.

What I had been doing is like this:

CAutoCFURL_AC theURL(::CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
CFSTR("/Users/ktam/Desktop"), kCFURLPOSIXPathStyle, true));

...

theURL = ::CFURLCreateWithFileSystemPath(kCFAllocatorDefault, CFSTR("/
Users/ktam/Library"), kCFURLPOSIXPathStyle, true);

now this results in a memory leak because the appropriate assignment
operator calls set:

template<class T>
inline TemplateAutoRef_AC<T>& TemplateAutoRef_AC<T>::operator=(T ptr)
{
	set(ptr);
}

and the set method

template<class T>
inline void TemplateAutoRef_AC<T>::set(T ptr)
{
	fReference = ptr;
}

does not release the previously assigned core foundation object.

The assignment operator:

template<class T>
inline TemplateAutoRef_AC<T>& TemplateAutoRef_AC<T>::operator=(const
TemplateAutoRef_AC<T>& ref)
{
	reset(ref.get());
	return *this;
}

seems to do the correct thing. Also the previous assignment operator does
not return a reference to the TemplateAutoRef_AC matching the return type.
Was this design intentional? If so, I would be interested in knowing why?

Kevin


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Macapp-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.