What is causing a BUS_ADRALN in [[NSBundle mainBundle] loadNibNamed? - iphone

Our application is occasionally getting a BUS_ADRALN. Looking at the crash log it shows the line [[NSBundle mainBundle] loadNibNamed:#"MyCell" owner:self options:nil]. I know a BUS_ADRALN is an invalid address, but I'm not seeing the problem. It works most of the time.
static NSString *buddyListCellId = #"MyCell";
cell = [tableView dequeueReusableCellWithIdentifier:buddyListCellId];
if (cell == nil) {
// the following line is what the crash is pointing to
[[NSBundle mainBundle] loadNibNamed:#"MyCell" owner:self options:nil];
cell = buddyListCell;
self.buddyListCell = nil;
}

That's pretty straightforward. What's the code leading up to it? Perhaps MyCell.nib is corrupt, try a clean and rebuild perhaps (your distribution on the device could be corrupt perhaps)

Related

Customizing UITableViewCell in Interface Builder

In two classes, I've subclassed UITableViewCell in order to do some major customization. I'd like to use a Xib file to keep the amount of UI layout code to a minimum. I'm coming across an odd exception:
if (!cell) {
if (indexPath.row == 0) {
cell = [[[SearchCellTop alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
NSArray* objects = [[NSBundle mainBundle] loadNibNamed:#"SearchCellTop" owner:cell options:nil];
cell = (SearchCellTop*)[objects objectAtIndex:0];
}
else {
cell = [[[SearchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
NSArray* objects = [[NSBundle mainBundle] loadNibNamed:#"SearchCell" owner:cell options:nil];
cell = (SearchCell*)[objects objectAtIndex:0];
}
}
This seems to work well for loading the Xibs. However as soon as I try doing something such as:
if (indexPath.row < [self tableView:tableView numberOfRowsInSection:indexPath.section])
((SearchCell*)cell).Product = [products objectAtIndex:indexPath.row];
I get -[UIAccessibiltyBundle setProduct:] unrecognized selector sent to instance
Everything indicates that 'cell' is of the correct type, however I'm stilling getting this error.
From Apple's developer documentation for the + (BOOL)loadNibNamed:(NSString *)aNibName owner:(id)owner method:
owner
The object to assign as the nib FIle's Owner. If the class of this object has an associated bundle, that bundle is searched for the specified nib file; otherwise, this method looks in the main bundle.
In your case, the owner should be nil (or a specific bundle, if associated).
In the code, change the calls to the loadNibNamed method as following:
NSArray* objects = [[NSBundle mainBundle] loadNibNamed:#"SearchCellTop" owner:nil options:nil];
NSArray* objects = [[NSBundle mainBundle] loadNibNamed:#"SearchCell" owner:nil options:nil];

Custom cell code showing leaks in iPhone

this may be a simple question but i can't figure the solution.Im creating a custom cell for a table view.When I'm testing the app instruments is showing leaks in code used for creating custom cell.I don't understand where i have to release the stuff for removing the leaks.Can anyone give me a hand for this.
This is the code i used for creating the custom cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID= #"Parcustom";
Parcustom *cell = (Parcustom *)[tableView dequeueReusableCellWithIdentifier:cellID];
if(cell==nil)
{
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"Parcustom" owner:nil options:nil];
for(id currentObject in nibObjects)
{
if([currentObject isKindOfClass: [Parcustom class]])
{
cell = (Parcustom *)currentObject;
}
}
} // Configure the cell...
cell.f1.text=[datedisplay objectAtIndex:indexPath.row];
cell.f2.text=[tips objectAtIndex:[[indexx objectAtIndex:indexPath.row]intValue]];
cell.selectionStyle=UITableViewCellSelectionStyleGray;
return cell;
}
The instrument is saying leak in the line NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"Parcustom" owner:nil options:nil];
How can i remove this leak.Can anyone help me please.
Replace below code with if block
NSArray *nibObjects = [[NSArray alloc] initWithArray:[[NSBundle mainBundle] loadNibNamed:#"Parcustom" owner:nil options:nil]];
for(id currentObject in nibObjects)
{
if([currentObject isKindOfClass: [Parcustom class]])
{
cell = (Parcustom *)currentObject;
}
}
[nibObjects release];
It seems there is no leak in the code presented. I suppose actual leak is somewhere in Parcustom class. Perhaps you don't release some ivar in Parcustom's dealloc. Maybe f1 or f2.
I don't see a leak in your code - Try running the static analyzer ("Analyze" in XCode's menu) and see if it reports a leak. If it does, you can expand the information about the leak and see the root cause.

Multiple SQLite Databases for Multiple Languages?

I would like to implement a multi language support for my app. So I created the Localizing.strings file and all that stuff and translated my interface. So far so good …
Now I want to duplicate my database in order to have a *.db-file for every single language. So I did and then I clicked via XCode on the "+" under the Localization tab. I now have a *.db-file in my en.lproj and de.lproj folder.
My problem: If I want to copy the db-files to the app's documents directory the *.db file is not available of course because it is in the *.lproj-folder. Is there any command to get the right lproj-folder?
To clarify my needs:
This doesn't work
[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"mydatabase.db"]
… this does:
[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"de.lproj/mydatabase.db"]
… but I don't want to add the "de.lproj" and "en.lproj" etc. manually. Is there any way to fix it dynamically?
just do the following:
NSString *dbpathResource =
[[NSBundle mainBundle] pathForResource:#"databaseName" ofType:#"db"];
and if you have your localized .db file in xx.lproj so the correct database will be taken.
What you want is the current language locale, the following code should return the code:
NSArray *languagesArray = [[NSUserDefaults standardUserDefaults] objectForKey:#"AppleLanguages"];
NSString *currentLanguage = [languagesArray objectAtIndex:0];
You can then do the following
[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.lproj/mydatabase.db", currentLanguage]];
You may want to check if the path exists and is a valid file, if not maybe use some default path like the one for English (en.lproj)
Edit: There is another way you can do this using NSLocale's preferred languages because then you get a list of the preferred languages, so some updated code for the first bit would be:
NSArray *languagesArray = [NSLocale preferredLanguages];
NSString *currentLanguage = [languagesArray objectAtIndex:0];
In the end, you'd end up with something like so:
NSString *pathComponent = [NSString stringWithFormat:#"%#.lproj/mydatabase.db", currentLanguage];
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:pathComponent];
NSString *activePath = nil; // This will store the active language file
// Check if the file exists...
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
activePath = path;
} else {
activePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"en.lproj/mydatabase.db"]; // Fallback
}
Please note, the above code is untested but should suffice. You may need to modify it a little...
Something like this:
NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString * rootPath = [[NSBundle mainBundle] resourcePath];
NSString * resourcePath = [[NSBundle mainBundle] pathForResource: #"mydatabase" ofType: #"db" inDirectory: rootPath forLocalization: language];

iphone: to much memory consumption and leak in case of table view

I am preparing custom cells like this
1) Created a custom cell with Xib
2) since I have to change content of lables in custom cell corresponding to values read from database. I cant reuse same cellIdentifier
static NSString *MyIdentifier = #"MyIdentifier";
MyIdentifier = [NSString stringWithFormat:#"Cell %d",indexPath.row];
NSString *offendersImagePath = [self applicationDocumentsDirectory];
offendersImagePath=[offendersImagePath stringByAppendingPathComponent:#"Images"];
CustomOffendersCell *cell = (CustomOffendersCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"CustomOffendersCell" owner:self options:nil];
cell = aCustomOffendersCell;
aCustomOffendersCell=nil;
}
NSMutableArray *tempArray;//=[[NSMutableDictionary alloc] init];
tempArray=[offendersNamesList objectAtIndex:indexPath.row];
the code goes like above when I checked this thing in instruments its showing leak for this line and high memory consumption
[[NSBundle mainBundle] loadNibNamed:#"CustomOffendersCell" owner:self options:nil];
can you suggest me some way to get rid of this
You should call "release" method for cell like "[aCustomOffendersCell release]".
Also you have to release all the data which you have alloc in cell like UILabel, UIImageView, etc... So, when you put it inside the cell, then release these all data.
Hope it will helpful to you. And let me know for more details.
you should call autorelease on aCustomOffendersCell instead of just assigning nil.
aCustomOffendersCell nil;
Use below
[aCustomOffendersCell autorelease];

Iphone SDK: pathFoResource:Nsstring problem -> playin MP3 files

I am trying to get 10 mp3 files in the resources folder and play them when a button is pressed. I have entered name of the mp3 files in a NSMutableArray and read each one after the button is pressed.
The problem is that pathForResource: is not working (returns nil). If i use the file name explicitly -like pathFoResource:#"song1.mp3" ofType:#"mp3"- but if i use pathForResource:valuem whwre valuem is an NSstring with value of song1
Hope you can help
Regards
NSString *cellValue0 = [listOfmp3 objectAtIndex:anumber];
NSString *valuem;
if ( [cellValue0 length] > 0 )
valuem = [cellValue0 substringToIndex:[cellValue0 length] - 4];
NSString *pdfPath2 = [[NSBundle mainBundle] pathForResource:valuem ofType:#"mp3"];
NSURL *url = [NSURL fileURLWithPath:pdfPath2];
Don't put the extension in the resource name, i.e.
[bundle pathForResource:#"song1" ofType:#"mp3"]; // correct
[bundle pathForResource:#"song1.mp3" ofType:#"mp3"]; // wrong — gets song1.mp3.mp3.
Check that valuem does not carry any extensions.
Sorry for my previous post, here is restatement of the problem in more clear terms:
Vladimir:
yes i log the valuem and it is giving the correct string
KennyTM:
Yes, it was a typo , in original code there is only #"song1"
the problem is that
if I use
[[NSBundle mainBundle] pathForResource:"song1" ofType:#"mp3"]; It is working
BUT If i use
[[NSBundle mainBundle] pathForResource:valuem ofType:#"mp3"];
It is NOT working
note that value is a string with #"song1"