Re: Non-NSObject object and garbage collection
Re: Non-NSObject object and garbage collection
- Subject: Re: Non-NSObject object and garbage collection
- From: "Ken Ferry" <email@hidden>
- Date: Tue, 12 Aug 2008 22:47:06 -0700
Hm. This is not going to be a great introduction to Mac OS X programming. :-)
It turns out that CGPDFContentStream, CGPDFOperatorTable, and
CGPDFScanner are not CFTypes. You cannot use any function intended
for CFTypes with them, including CFRetain, CFRelease and
CFMakeCollectable. You cannot put them in arrays or dictionaries.
This is unusual, and should be documented. Please file a bug at
bugreporter.apple.com.
Anywho, you cannot use CFMakeCollectable. When you're finished with
these objects call the appropriate release function for each type.
That is, one of CGPDFContentStreamRelease, CGPDFOperatorTableRelease
or CGPDFScannerRelease.
Regarding breakpoints, see
<http://developer.apple.com/documentation/DeveloperTools/Conceptual/XcodeDebugging/500-Managing_Program_Execution/chapter_8_section_2.html>.
-Ken
On Tue, Aug 12, 2008 at 9:22 PM, Oleg Krupnov <email@hidden> wrote:
> On Wed, Aug 13, 2008 at 6:56 AM, Ken Ferry <email@hidden> wrote:
>> What does the stack look like when you break on
>> auto_refcount_underflow_error? After you break, you can do "call
>> (void)CFShow(<number goes here>)" with the number printed out in the
>> log message. That should hopefully give you the type of the object,
>> which might help you determine where the issue is.
>
>
> Actually, I don't know how to "break on something". Would you please
> tell me? Also, I have always been wondering how do I set compiler
> flags in XCode...
>
>> What do you mean when you say that only one object causes the problem?
>> If that means you know what object it is, then can you show the code
>> where you work with it?
>
> I mean that when the function gets first executed in debugger, I
> record on paper all the hex addresses of the few objects I create, and
> then when the error is issued later on, I check the address and see
> what is the object by its address.
>
> Here's the code. It is a voyeur-like pdf parser.
>
> CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)pdfData);
> if (provider != nil)
> {
> CFMakeCollectable(provider);
> CGPDFDocumentRef document = CGPDFDocumentCreateWithProvider(provider);
> if (document != nil)
> {
> CFMakeCollectable(document);
> size_t pageCount = CGPDFDocumentGetNumberOfPages(document);
> if (pageCount > 0)
> {
> CGPDFPageRef page = CGPDFDocumentGetPage(document, 1);
> if (page != nil)
> {
> // *******TODO: the page object causes
> the reference count underflow
> CFMakeCollectable(page);
>
> CGPDFContentStreamRef contentStream =
> CGPDFContentStreamCreateWithPage(page);
> if (contentStream != nil)
> {
> CFMakeCollectable(contentStream);
> CGPDFOperatorTableRef
> operatorTable = CGPDFOperatorTableCreate();
> if (operatorTable != nil)
> {
> CFMakeCollectable(operatorTable);
> // the op_xxx are just
> functions defined in the .m file
>
> CGPDFOperatorTableSetCallback(operatorTable, "q", &op_q);
>
> CGPDFOperatorTableSetCallback(operatorTable, "Q", &op_Q);
>
> CGPDFOperatorTableSetCallback(operatorTable, "cm", &op_cm);
>
> CGPDFScannerRef scanner
> = CGPDFScannerCreate(stream, operatorTable, self);
> if (scanner != nil)
> {
>
> CFMakeCollectable(scanner);
>
> CGPDFScannerScan(scanner);
> }
> }
> . }
> - Hide quoted text -
> }
> }
> }
> }
>
>
>> -Ken
>>
>> On Tue, Aug 12, 2008 at 8:46 PM, Oleg Krupnov <email@hidden> wrote:
>>> I have added a call to CFMakeCollectable each time after I create a CF
>>> object like this
>>>
>>> CFSomeTypeRef a = CFCreateSomeType();
>>> if (a != nil)
>>> {
>>> CFMakeCollectable(a);
>>> }
>>>
>>> But I keep getting this runtime message in the console:
>>> malloc: reference count underflow for 0x10773c0, break on
>>> auto_refcount_underflow_error to debug.
>>>
>>> All CF objects I create are local vars in scope of one single
>>> function, no instance or global vars. I create several CF objects,
>>> some of them are used as arguments for other functions. The error is
>>> issued only for one of the CF objects I create, the other work fine.
>>>
>>> What can be the problem?
>>>
>>> On Mon, Aug 11, 2008 at 3:25 PM, Antonio Nunes <email@hidden> wrote:
>>>> On 11 Aug 2008, at 13:14, Antonio Nunes wrote:
>>>>
>>>>>> Will it cause a memory leak if I treat the Quartz object in the same
>>>>>> way as I do to all my NSObject descendants, i.e. no retains and rely
>>>>>> only on garbage collection?
>>>>>
>>>>> You either take care of the object's life time manually as you would in a
>>>>> non-garbage collected environment, or you explicitly move it into the realm
>>>>> of the garbage collector. Lookup "CFMakeCollectable" and "NSMakeCollectable"
>>>>> in the docs.
>>>>
>>>> I should add: read the "Memory Management Semantics" in "Using Core
>>>> Foundation with Garbage Collection" in the "Garbage Collection Programming
>>>> Guide", which has a thorough explanation of how to handle CF objects under
>>>> GC.
>>>>
>>>> António
>>>>
>>>> ----------------------------------------------------
>>>> It isn't so important to do great things,
>>>> as to do what you do with great love.
>>>> ----------------------------------------------------
>>>>
>>>>
>>>>
>>>>
>>> _______________________________________________
>>>
>>> Cocoa-dev mailing list (email@hidden)
>>>
>>> Please do not post admin requests or moderator comments to the list.
>>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>>
>>> Help/Unsubscribe/Update your Subscription:
>>>
>>> This email sent to email@hidden
>>>
>>
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden