Memory leak in TBXML iOS Parser - iphone

I am using TBXML for XML feed parsing and instruments to detect memory leaks.
Instruments tells me there is a memory leak in function
- (TBXMLAttribute*) nextAvailableAttribute {
currentAttribute++;
if (!currentAttributeBuffer) {
currentAttributeBuffer = calloc(1, sizeof(TBXMLAttributeBuffer));
currentAttributeBuffer->attributes = (TBXMLAttribute*)calloc(MAX_ATTRIBUTES,sizeof(TBXMLAttribute));
currentAttribute = 0;
} else if (currentAttribute >= MAX_ATTRIBUTES) {
currentAttributeBuffer->next = calloc(1, sizeof(TBXMLAttributeBuffer));
currentAttributeBuffer->next->previous = currentAttributeBuffer;
currentAttributeBuffer = currentAttributeBuffer->next;
currentAttributeBuffer->attributes = (TBXMLAttribute*)calloc(MAX_ATTRIBUTES,sizeof(TBXMLAttribute));
currentAttribute = 0;
}
return &currentAttributeBuffer->attributes[currentAttribute];
}
at the line
currentAttributeBuffer->attributes = (TBXMLAttribute*)calloc(MAX_ATTRIBUTES,sizeof(TBXMLAttribute));
Does any one solved it before?

BEWARE.... instruments is telling you WHERE the leaked memory was originally created, NOT that the issue is in that line... for instance, if you do something with the attribute returned and leak it elsewhere, instruments will show THE line above, where it was created, NOT where you leaked it....

calloc is a C function to allocate memory. You should call free currentAttributeBuffer; when you're finished with it. That will fix the memory leak.

Related

Unsolvable memory leak IPhone

I am new to IPhone programming and am I having trouble solving the following memory leak.
while(numDeckCounter < numDecks){
int cardCounter=1;
for (int i =1; i<=52; i++) {
tempCard = [Card new]; //leaks tool says that this is leaking object
if(i>=1 && i<=13)
{
tempCard.suit = CLUBS;
tempCard.faceValue = cardCounter;
[deckArr addObject:tempCard]; //reference count 2
cardCounter++;
}
else if(i>=14 && i<=26)
{
tempCard.suit = DIAMONDS;
tempCard.faceValue = cardCounter;
[deckArr addObject:tempCard];
cardCounter++;
}
else if(i>=27 && i<=39)
{
tempCard.suit = HEARTS;
tempCard.faceValue = cardCounter;
[deckArr addObject:tempCard];
cardCounter++;
}
else
{
tempCard.suit = SPADES;
tempCard.faceValue = cardCounter;
[deckArr addObject:tempCard];
cardCounter++;
}
if(cardCounter ==14){
cardCounter=1;
}
[tempCard release]; //this causes an EXC_BAD_ACCESS -reference count should be 1
}
numDeckCounter++;
}
I was under the impression that adding an object to the array would increase its reference count by one, then it would be safe to release the object you just added because it would not be deallocated until the array was released bumping which would then release each object in the array. This is when the object should finally be deallocated.
When I add the [tempCard release]; it crashes my app because it can't access the memory location because it has already been deallocated.
From everything I have read, I think what I said above is true. Someone please correct me if I am wrong. Thanks.
I don't see any leaks in the code you presented. (There are some opportunities to slim it down, though, say by moving the identical operations out of the conditionals).
The Leaks tool output is pretty tricky to read. Is it possible that the Card object is leaking one of it's ivars?
Instead of leaks, run static analysis on your product (Product->Analyze). I think it will flag a different part of your code.
Perhaps try [[Card alloc] init] instead of [Card new]. This is just a guess. However trying the IMO more common method of object creation could be helpful.
Check this out: Use of alloc init instead of new
You could also try removing all the code for adding the Cards to the array. So you'd essentially have:
card = [Card new];
[card release];
This could help you find memory issues associated w/ the array retaining the object perhaps?

Memory Leak when using Pointer to AudioUnitSampleType in Struct - calloc

I am coding an audio app for the iphone where I need to use some C code to deal with the audio files. In short, I have a memory leak that is causing the app to crash after so many files have been loaded. The problem is related to a Struct that I create that holds the audio files when read in. The Struct is created as follows;
typedef struct {
UInt32 frameCount; // the total number of frames in the audio data
UInt32 sampleNumber; // the next audio sample to play
BOOL isStereo; // set to true if there is data audioDataRight member
AudioUnitSampleType *audioDataLeft; // complete left channel of audio data read from file
AudioUnitSampleType *audioDataRight; // complete right channel of audio data read file
} soundStruct, *soundStructPtr;
The Struct is then Initialized in the header like this;
soundStruct phraseSynthStructArray[3];
I then attempt to join two files that have been read into phraseSynthStructArray[phrase1Index] and phraseSynthStructArray[phrase2Index] and put the combined file into phraseSynthStructArray[synthPhraseIndex] like this;
- (BOOL) joinPhrases:(UInt32)phrase1Index phrase2Index:(UInt32)phrase2Index synthPhraseIndex:(UInt32)synthPhraseIndex{
// get the combined frame count
UInt64 totalFramesInFile = inArray[phrase1Index].frameCount + inArray[phrase2Index].frameCount;
//now resize the synthPhrase slot buffer to be the same size as both files combined
// phraseOut is used to hold the combined data prior to it being passed into the soundStructArray slot
free(phraseSynthStructArray[synthPhraseIndex].audioDataLeft);
phraseSynthStructArray[synthPhraseIndex].audioDataLeft = NULL;
phraseSynthStructArray[synthPhraseIndex].frameCount = 0;
phraseSynthStructArray[synthPhraseIndex].frameCount = totalFramesInFile;
phraseSynthStructArray[synthPhraseIndex].audioDataLeft = (AudioUnitSampleType *) calloc(totalFramesInFile, sizeof (AudioUnitSampleType));
for (UInt32 frameNumber = 0; frameNumber < inArray[phrase1Index].frameCount; ++frameNumber) {
phraseSynthStructArray[synthPhraseIndex].audioDataLeft[frameNumber] = phraseSynthStructArray[phrase1Index].audioDataLeft[frameNumber];
}
UInt32 sampleNumber=0;
for (UInt32 frameNumber = phraseSynthStructArray[phrase1Index].frameCount; frameNumber < totalFramesInFile; ++frameNumber) {
phraseSynthStructArray[synthPhraseIndex].audioDataLeft[frameNumber] = phraseSynthStructArray[phrase2Index].audioDataLeft[sampleNumber];
sampleNumber++;
}
return YES;
}
This all works fine and the resulting file is joined and can be used. The isuue I am having is when I allocate the memory here, phraseSynthStructArray[synthPhraseIndex].audioDataLeft = (AudioUnitSampleType *) calloc(totalFramesInFile, sizeof (AudioUnitSampleType)); then next time the method is called, this memory leaks each time and eventually crashes the app. The reason I need to allocate the memory here is because the memory has to be resized to accomodate the joined file which varies in length depending on the size of the input files.
I cannot free the memory after the operation as its needed elsewhere after the method has been called and I have tried to free it before (in joinPhrases method above), but this does not seem to work. I have also tried using realloc to free/reallocate the memory by passing the pointer to the previously allocated memory but this casues a crash stating EXEC_BAD_ACCESS.
I am not a seasoned C programmer and Cannot figure out what I am doing wrong here to cause the leak. I would appreciate some advice to help me track down this issue as I have been banging my head against this for days with no joy. I have read thats its a bad idea to have Pointers in Structs, could this be the root of my problem?
Thanks in advance,
K.
Maybe this helps:
- (BOOL) joinPhrases:(UInt32)phrase1Index phrase2Index:(UInt32)phrase2Index synthPhraseIndex:(UInt32)synthPhraseIndex{
// get the combined frame count
UInt64 totalFramesInFile = inArray[phrase1Index].frameCount + inArray[phrase2Index].frameCount;
. . .
void* old_ptr = phraseSynthStructArray[synthPhraseIndex].audioDataLeft;
phraseSynthStructArray[synthPhraseIndex].audioDataLeft = (AudioUnitSampleType *) calloc(totalFramesInFile, sizeof (AudioUnitSampleType));
if( old_ptr ) free(old_ptr);
. . .
return YES;
}
And make sure that there is no garbage in phraseSynthStructArray[synthPhraseIndex]

How does one release memory correctly in the attached C array?

I'm just trying to work out why the following code is leaking memory and I have a funny feeling that i'm not releasing the array memory correctly. This is a C function in a wider objective-c app and I'm not native to C... i've tried just using free() on the array, but have a feeling this isn't the whole story...
Could someone have a look and see what I'm missing here. Thanks!
CFIndex theNumberOfSettings = 3;
CTParagraphStyleSetting theSettings[3] =
{
{kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment), &alignment},
{kCTParagraphStyleSpecifierLineSpacing, sizeof(lineSpacing), &lineSpacing},
{kCTParagraphStyleSpecifierHeadIndent, sizeof(headIndent), &headIndent}
};
CTParagraphStyleRef theParagraphRef = CTParagraphStyleCreate(theSettings, theNumberOfSettings);
CFAttributedStringSetAttribute(attrString, CFRangeMake(0, CFAttributedStringGetLength(attrString)-1), kCTParagraphStyleAttributeName, theParagraphRef);
CFRelease(theParagraphRef);
free(theSettings);
You don't free memory that's not allocated on the heap.

How do I free() after malloc() when the result of malloc() is returned by the function?

I have the following instance method (adapted from Listing 3-6 of the Event Handling section in the iPhone Application Programming Guide):
- (CGPoint)originOfTouch:(UITouch *)touch
{
CGPoint *touchOriginPoint = (CGPoint *)CFDictionaryGetValue(touchOriginPoints, touch);
if (touchOriginPoint == NULL)
{
touchOriginPoint = (CGPoint *)malloc(sizeof(CGPoint)); // leaks
CFDictionarySetValue(touchOriginPoints, touch, touchOriginPoint);
*touchOriginPoint = [touch locationInView:touch.view];
}
return *touchOriginPoint;
}
Every once in a while my app leaks 16 Bytes as a result of the call to malloc(). I'm not sure how to return touchOriginPoint while free()ing it as well.
If you do not care a minor performance loss, use an NSMutableDictionary and store the point as an NSValue:
NSValue* touchOriginPointValue = [touchOriginPoints objectForKey:touch];
if (touchOriginPointValue == nil) {
touchOriginPointValue = [NSValue valueWithCGPoint:[touch locationInView:touch.view]];
[touchOriginPoints setObject:touchOriginPointValue forKey:touch];
}
return [touchOriginPointValue CGPointValue];
If you must use the CFDictionary approach, you have to find a place to free those malloc-ed memory when the values are not needed. Therefore, you have to pass the values callbacks when creating the dictionary
static void free_malloced_memory (CFAllocatorRef allocator, const void *value) {
free((void*)value);
}
static const CFDictionaryValueCallBacks values_callbacks = {0, NULL, free_malloced_memory, NULL, NULL};
...
touchOriginPoints = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, & values_callbacks);
...
If you must return the malloc'd value from the function, then you have passed the responsibility for freeing the memory to the calling function, or one of its callers.
Since we can't see the calling functions, we can't diagnose any more.
If you are going to be returning an object that is allocated, then either you need to have the caller free() it, or else you need to be using some kind of garbage collection (so it gets freed automatically).
you don't actually return a pointer, the value is copied to a temp value when it is returned, so you aren't really returning the allocation at all, the problem is that you just aren't freeing it either, you add the allocation to the dictionary and just leave it there?
is there like an EndOfTouch function? where you remove the touch from the dictionary? if there is, call free on your allocation there and you should be fine

How to release memory created from CFStringTokenizerCreate?

I use CFRelease to release the CFStringTokenizerRef obtained from CFStringTokenizerCreate call. But instruments is reporting memory leak at around this area. Am I missing something?
CFStringTokenizerRef tokenRef = CFStringTokenizerCreate(NULL,
(CFStringRef)contents,
CFRangeMake(0, contents.length),
kCFStringTokenizerUnitWordBoundary,
NULL);
CFStringTokenizerTokenType tokenType;
// leak reported here
while ((tokenType = CFStringTokenizerAdvanceToNextToken(tokenRef)) != kCFStringTokenizerTokenNone)
}
CFRelease(tokenRef);
CFStringTokenizerCreate follows the Create Rule. So you are calling as expected.
There seems to be a missing brace after the while, this the CFRelease is not in the same scope. Is it a copy/paste error ?