Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Sigbus 10 on NSString Allocation

Sigbus 10 on NSString Allocation
Thread Tools
Mac Elite
Join Date: Jan 2001
Location: New York
Status: Offline
Reply With Quote
Jul 27, 2001, 03:18 PM
 
- (void)applicationDidFinishLaunching NSNotification *)notification
{
int i,i1,i2,i3;
NSDictionary *d;
NSMutableString *s;
NSMutableString *tCheck;
NSString *fs = @"/users/shared/BeginData"; //quits here

On the last line of the above I'm getting a Sigbus 10 and my application is quitting. I don't 'get it' at all. What am I doing wrong?

Any ideas?
     
Grizzled Veteran
Join Date: Apr 2001
Status: Offline
Reply With Quote
Jul 27, 2001, 04:17 PM
 
That certainly looks like it should work.

Wade
     
davecom  (op)
Mac Elite
Join Date: Jan 2001
Location: New York
Status: Offline
Reply With Quote
Jul 27, 2001, 04:21 PM
 
I did some more testing on it and I found that any variable being set to anything doesn't work! For example even setting the integers to 0 exits on a Sigbus 10:

- (void)applicationDidFinishLaunching NSNotification *)notification
{
int i = 0,i1 = 0,i2 = 0,i3 = 0; //quits here
NSDictionary *d;
NSMutableString *s;
NSMutableString *tCheck;
NSString *fs = @"/users/shared/BeginData";


What the heck is going on?!

Any suggestions greatly gratified.
     
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status: Offline
Reply With Quote
Jul 27, 2001, 10:39 PM
 
Whatever the problem is, it's not the code you posted. There's something else *very* weird going on.

So, the first cardinal rule when something impossibly weird is happening: clean the project, and build from scratch.

Past that, dunno. Make sure there are no warnings. Check some of the obvious stuff, #importing the right headers, are local vars shadowing instance vars etc (though those would typically result in a warning). Something seems to be getting messed up with the stack to the point that compiler-generated code is causing a segfault, so it's possible you're doing something screwy in other code that's causing a problem later on (overrunning an array alloced with alloca maybe)?
Sometimes overrunning arrays that are static variables (i.e. smashing random static memory) can result in all sorts of wacky problems. You might also post the backtrace from gdb; maybe that could give some hints.

If you change your code so that it doesn't initialize anything on the same line it's been declared, i.e. NSString *fs; and later fs = @"....";
what happens?
     
davecom  (op)
Mac Elite
Join Date: Jan 2001
Location: New York
Status: Offline
Reply With Quote
Jul 27, 2001, 10:57 PM
 
Okay, I cleaned and built from scratch, same thing happened. No warnings are received. Something very funny is indeed going on, quite a mystery. #importing seems right, there are no local variables shadowing global variables.

If you change your code so that it doesn't initialize anything on the same line it's been declared, i.e. NSString *fs; and later fs = @"....";
what happens?
If I do this it gives the sigbus 10 on the line that does fs = @"...."; - not the line that goes NSString *fs;

This is crazy!? Any suggestions more than welcome, this is making me quite wary of Cocoa, even though my previous projects went fine. Please help.
     
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status: Offline
Reply With Quote
Jul 28, 2001, 09:49 AM
 
Okay... it's dying on the first executable statement in the method, or so it seems. Lines that just declare variables aren't executable statements, so there's no code generated by the compiler, so there's nothing there to crash on.

Can you post the GDB backtrace?

If you comment out the entire method, does the app run then? It may be a problem in another piece of code but the debugger is confused about something.

Try implementing the applicationWillFinishLaunching method and just put an NSLog in there, to see if it gets that far.

Is there any way you have infinite recursion going on? i.e. are you doing anything inside the method that might result in applicationDidFinishLaunching getting called again? That can sometimes seem to result in an error like this because the program is trying to allocate stack space for another function call but there's no more room on the stack.

Whatever the problem is, I don't think it's the Cocoa frameworks :-) It usually turns out to be something pretty silly.
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Jul 28, 2001, 09:59 AM
 
Originally posted by davecom:
<STRONG>This is crazy!? Any suggestions more than welcome, this is making me quite wary of Cocoa, even though my previous projects went fine. Please help.</STRONG>
You probably will need to post more code than that for people to help, for example anything that's being executed before that. I know someone who was having an app (foundation tool) with no code in it sigbus on launch, or so he said, which was odd. *shrug*
     
davecom  (op)
Mac Elite
Join Date: Jan 2001
Location: New York
Status: Offline
Reply With Quote
Jul 28, 2001, 01:29 PM
 
I posted the GDB Backtrace on the bottom and then the comments from a regular run below that(I think that's a backtrace, not 100% sure). I'll post the entire method below. I checked and I don't see any infinite recursion. I don't think I understood how to use the debugger before, but now I think I get it. I think I thought it was stopping on the first red line, but the first red line was just the first breakpoint with executable code. Now I think it stops on the first if statement. It says program received signal "EXC_BAD_ACCESS" on the first if statement. It doesn't exit on Sigbus 10 in debug mode, but does in regular run mode. I couldn't find anything wrong with my if statement, since I used the same one in a different program that runs fine.

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
#import <font color = red>"Controller.h"</font>

<font color = purple>@implementation</font> Controller

- (<font color = green>void</font>)applicationWillFinishLaunching NSNotification *)aNotification
{
NSLog(<font color = orange>@"Got <font color = green>this</font> far"</font>);
}
- (<font color = green>void</font>)applicationDidFinishLaunching NSNotification *)notification
{
<font color = green>int</font> i,i1,i2,i3;
NSDictionary *d;
NSMutableString *s;
NSMutableString *tCheck;
NSString *fs = <font color = orange>@"/users/shared/BeginData"</font>;
mt = [[menuTracker alloc] init];
[<font color = purple>self</font> setUpTitles];
<font color = green>if</font> ([[NSUserDefaults standardUserDefaults] boolForKey:<font color = orange>@"hasRun"</font>] == <font color = purple>YES</font>) <font color = brown>//check <font color = green>if</font> run before</font>
{
<font color = green>if</font> ((d = [NSMutableDictionary dictionaryWithContentsOfFile:fs]) != <font color = green>nil</font>) <font color = brown>//<font color = green>if</font> user didn't delete file</font>
{
<font color = green>for</font>(i=<font color = blue>0</font>;i&lt;<font color = blue>50</font>;i++) <font color = brown>//go through all and arrange appropriately</font>
<font color = green>if</font> ([d objectForKey:titles[i]] != <font color = green>nil</font>)
{
[tCheck setString:[d objectForKey:titles[i]]];
<font color = green>if</font> ([tCheck substringWithRange NSRange)NSMakeRange(<font color = blue>0</font>,<font color = blue>1</font>)] == <font color = orange>@"a"</font>)
{
[tCheck deleteCharactersInRange NSRange)NSMakeRange(<font color = blue>0</font>,<font color = blue>1</font>)]; <font color = brown>//delete prefix</font>
s = [d objectForKey aths[i]];
[mt addApplication NSString *)tCheck withPath NSString *)s]; <font color = brown>//add app to menu tracker</font>
i1++;
[mt setAppNumber:i1];
}
<font color = green>if</font> ([tCheck substringWithRange NSRange)NSMakeRange(<font color = blue>0</font>,<font color = blue>1</font>)] == <font color = orange>@"d"</font>)
{
[tCheck deleteCharactersInRange NSRange)NSMakeRange(<font color = blue>0</font>,<font color = blue>1</font>)]; <font color = brown>//delete prefix</font>
s = [d objectForKey aths[i]];
[mt addDocument:tCheck withPath:s]; <font color = brown>//add doc to menu tracker</font>
i2++;
[mt setDocNumber:i2];
}
<font color = green>if</font> ([tCheck substringWithRange NSRange)NSMakeRange(<font color = blue>0</font>,<font color = blue>1</font>)] == <font color = orange>@"u"</font>)
{
[tCheck deleteCharactersInRange NSRange)NSMakeRange(<font color = blue>0</font>,<font color = blue>1</font>)]; <font color = brown>//delete prefix</font>
s = [d objectForKey aths[i]];
[mt addURL:tCheck withPath:s]; <font color = brown>//add URL to menu tracker</font>
i3++;
[mt setURLNumber:i3];
}
}
}
}
<font color = green>else</font>
{
[mt addApplication:<font color = orange>@"Calculator<font color = red>"</font> withPath:<font color = orange>@"</font>/Applications/Calculator"</font>];
[mt addApp];
[[NSUserDefaults standardUserDefaults] setBool:<font color = purple>YES</font> forKey:<font color = orange>@"hasRun"</font>]; <font color = brown>//say run before</font>
}
[s release];
[fs release];
[d release];
[tCheck release];
}
</font>[/code]

GDB Backtrace:
GNU gdb 5.0-20001113 (Apple version gdb-186.1) (Sun Feb 18 01:18:32 GMT 2001) (UI_OUT)
run
Copyright 2000 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "powerpc-apple-macos10".
Reading symbols for shared libraries ... done
[Switching to thread 1 (process 317 thread 0x1903)]
MANPATH: Undefined variable.
Reading symbols for shared libraries .................................... done
Jul 28 14:23:56 Begin[317] Got this far
(gdb)


Run Comments:
Jul 28 14:25:01 Begin[329] Got this far

Begin.app has exited due to signal 10 (SIGBUS).

[ 07-28-2001: Message edited by: davecom ]
     
davecom  (op)
Mac Elite
Join Date: Jan 2001
Location: New York
Status: Offline
Reply With Quote
Jul 28, 2001, 01:31 PM
 
I forgot to thank everyone for their help so far. Also I'm sorry my formatting and indentations didn't come out on the copied and pasted code above.
     
Dedicated MacNNer
Join Date: Feb 2001
Status: Offline
Reply With Quote
Jul 28, 2001, 02:19 PM
 
That is really weird. Can you build and run Apple's example code? If so, you might try creating a new project and moving the source files over, maybe a project setting got hosed. If not, your developer tools might be corrupted somehow and you can try reinstalling them. Also you might want to post this on the macosx-dev list at omnigroup.com and see if anybody there can help.
     
davecom  (op)
Mac Elite
Join Date: Jan 2001
Location: New York
Status: Offline
Reply With Quote
Jul 28, 2001, 02:39 PM
 
That's really what I was afraid of!

Why does this always happen to me?

I hoped it was some stupid error that someone would catch.

TextEdit builds and runs fine.

Okay upon moving the files to a new project, the same thing happens.

I'll be happy to send somebody all the source code at this point, if they're interested.

I think I'll stop Cocoa development for the time being. This has really shaken me up.
     
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status: Offline
Reply With Quote
Jul 28, 2001, 02:51 PM
 
From that code, it looks like tCheck is an uninitialized variable. Adding tCheck = [NSMutableString string]; before the first if statement should do it (or initializing it similarly on the declaration line).

Other notes...

Class names by convention start with upper case letters, so unless menuTracker is a variable typed as 'Class'.... :-)

NSMakeRange is already typed to return an NSRange, so you don't need to cast it again. [tCheck deleteCharactersInRange:NSMakeRange(0,1)] should be fine.

NSString has a -hasPrefix: method; [tCheck hasPrefix:@"a"] would be an easier way than what you're doing.

You don't need to release any of the variables, since they're all autoreleased. You only need a -release (or -autorelease) if in your code you've explicitly used either +alloc (or the deprecated +new), -copy, -mutableCopy, or -retain yourself. Everything else is pretty much already autoreleased, and the memory will be collected without you doing anything else. For example, if you initialized tCheck with [[NSMutableString alloc] init] rather than [NSMutableString string], you would have to have the [tCheck release] in there.

The values coming out of the dictionary will be NSString instances, not NSMutableString, so the "s" local variable should technically be typed to match.
     
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status: Offline
Reply With Quote
Jul 28, 2001, 03:03 PM
 
One other note -- normally if you compile with optimizations (-O), the compiler will pick up uninitialized variables and give you a warning. -O should be on normally unless you're building in debug mode. It's a quirk of gcc that the uninitialized variable check is in the optimizer (since that's the piece of code that goes through and figures out where variables are used to see if things can be optmized in any way).
     
davecom  (op)
Mac Elite
Join Date: Jan 2001
Location: New York
Status: Offline
Reply With Quote
Jul 28, 2001, 03:06 PM
 
I added all the changes you suggested, except for the naming thing, I think I picked that up in a Java book or something like that. I took out the casting, even though I like it since it clarifies the code for me. I think you solved the SigBus error and therefor I need to thank you. Email me for a free license to URL Box and I'll send you a free license to this program too., now I get this:

Jul 28 16:02:47 Begin[671] Got this far

Begin.app has exited due to signal 11 (SIGSEGV).


The code now looks as follows
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
#import <font color = red>"Controller.h"</font>

<font color = purple>@implementation</font> Controller

- (<font color = green>void</font>)applicationWillFinishLaunching NSNotification *)aNotification
{
NSLog(<font color = orange>@"Got <font color = green>this</font> far"</font>);
}

- (<font color = green>void</font>)applicationDidFinishLaunching NSNotification *)notification
{
<font color = green>int</font> i,i1,i2,i3;
NSDictionary *d;
NSString *s;
NSMutableString *tCheck = [NSMutableString string];
NSString *fs = <font color = orange>@"/users/shared/BeginData"</font>;
mt = [[menuTracker alloc] init];
[<font color = purple>self</font> setUpTitles];
<font color = green>if</font> ([[NSUserDefaults standardUserDefaults] boolForKey:<font color = orange>@"hasRun"</font>] == <font color = purple>YES</font>) <font color = brown>//check <font color = green>if</font> run before</font>
{
<font color = green>if</font> ((d = [NSMutableDictionary dictionaryWithContentsOfFile:fs]) != <font color = green>nil</font>) <font color = brown>//<font color = green>if</font> user didn't delete file</font>
{
<font color = green>for</font>(i=<font color = blue>0</font>;i&lt;<font color = blue>50</font>;i++) <font color = brown>//go through all and arrange appropriately</font>
<font color = green>if</font> ([d objectForKey:titles[i]] != <font color = green>nil</font>)
{
[tCheck setString:[d objectForKey:titles[i]]];
<font color = green>if</font> ([tCheck hasPrefix:<font color = orange>@"a"</font>])
{
[tCheck deleteCharactersInRange:NSMakeRange(<font color = blue>0</font>,<font color = blue>1</font>)]; <font color = brown>//delete prefix</font>
s = [d objectForKey aths[i]];
[mt addApplication NSString *)tCheck withPath NSString *)s]; <font color = brown>//add app to menu tracker</font>
i1++;
[mt setAppNumber:i1];
}
<font color = green>if</font> ([tCheck hasPrefix:<font color = orange>@"d"</font>])
{
[tCheck deleteCharactersInRange NSRange)NSMakeRange(<font color = blue>0</font>,<font color = blue>1</font>)]; <font color = brown>//delete prefix</font>
s = [d objectForKey aths[i]];
[mt addDocument:tCheck withPath:s]; <font color = brown>//add doc to menu tracker</font>
i2++;
[mt setDocNumber:i2];
}
<font color = green>if</font> ([tCheck hasPrefix:<font color = orange>@"u"</font>])
{
[tCheck deleteCharactersInRange NSRange)NSMakeRange(<font color = blue>0</font>,<font color = blue>1</font>)]; <font color = brown>//delete prefix</font>
s = [d objectForKey aths[i]];
[mt addURL:tCheck withPath:s]; <font color = brown>//add URL to menu tracker</font>
i3++;
[mt setURLNumber:i3];
}
}
}
}
<font color = green>else</font>
{
[mt addApplication:<font color = orange>@"Calculator<font color = red>"</font> withPath:<font color = orange>@"</font>/Applications/Calculator"</font>];
[mt addApp];
[[NSUserDefaults standardUserDefaults] setBool:<font color = purple>YES</font> forKey:<font color = orange>@"hasRun"</font>]; <font color = brown>//say run before</font>
}
[mt release];
}
</font>[/code]
     
davecom  (op)
Mac Elite
Join Date: Jan 2001
Location: New York
Status: Offline
Reply With Quote
Jul 28, 2001, 03:10 PM
 
Where do I set this optimization thing. I found a popup menu in build settings with Development Optimization Level and Deployment Optimization Level. The choices are Default, No Optimization, Level 1, Level 2, and Level 3. My Development is on No Optimization and my Deployment is on Default.
     
Fresh-Faced Recruit
Join Date: Apr 2001
Location: Versailles, France
Status: Offline
Reply With Quote
Jul 28, 2001, 03:29 PM
 
Originally posted by davecom:
Begin.app has exited due to signal 11 (SIGSEGV).
Can you post the backtrace ?

NSDictionary *d;
NSString *s;
NSMutableString *tCheck = [NSMutableString string];
NSString *fs = &lt;font color=orange&gt;@"/users/shared/BeginData"&lt;/font&gt;;
mt = [[menuTracker alloc] init];
[&lt;font color=purple&gt;self&lt;/font&gt; setUpTitles];
&lt;font color=green&gt;if&lt;/font&gt; ([[NSUserDefaults standardUserDefaults] boolForKey:&lt;font color=orange&gt;@"hasRun"&lt;/font&gt;] == &lt;font color=purple&gt;YES&lt;/font&gt; ) &lt;font color=brown&gt;//check &lt;font color=green&gt;if&lt;/font&gt; run before&lt;/font&gt;
{
&lt;font color=green&gt;if&lt;/font&gt; ((d = [NSMutableDictionary dictionaryWithContentsOfFile:fs]) != &lt;font color=green&gt;nil&lt;/font&gt; )
Looks like you should be using [NSDictionary dictionaryWithContentsOfFile:fs], not NSMutableDictionary, since you write : NSDictionary *d .... shouldn't you ?
     
davecom  (op)
Mac Elite
Join Date: Jan 2001
Location: New York
Status: Offline
Reply With Quote
Jul 28, 2001, 03:38 PM
 
I changed the dictionary thing here's the backtrace:

(gdb) backtrace
#0 0x7081ab98 in objc_msgSend ()
#1 0x709815e8 in NSPopAutoreleasePool ()
#2 0x7098147c in -[NSAutoreleasePool release] ()
#3 0x70d754b0 in -[NSApplication run] ()
#4 0x70d9799c in NSApplicationMain ()
#5 0x00003bcc in main (argc=1, argv=0xbffffd4c) at main.m:5/Users/dave/Documents/Projects/Begin/
#6 0x00003af0 in _start ()
#7 0x00003930 in start ()
#8 0x00000001 in ?? ()
(gdb)


Could someone tell me where I can learn how to read this thing?
     
Fresh-Faced Recruit
Join Date: Apr 2001
Location: Versailles, France
Status: Offline
Reply With Quote
Jul 28, 2001, 03:44 PM
 
Originally posted by davecom:

#2 0x7098147c in -[NSAutoreleasePool release] ()
Maybe try to delete [mt release]; ... seems that the object is autoreleased.

Quick tip : when you hit "debug" in Project Builder, the backtrace is "graphical" and when you click on one of its lines, PB colors the line when the problem occured.
You should be able to quickly discover the leak using that.
     
davecom  (op)
Mac Elite
Join Date: Jan 2001
Location: New York
Status: Offline
Reply With Quote
Jul 28, 2001, 03:55 PM
 
It seems that the program is getting the:
Program received signal: "EXC_BAD_ACCESS" on the second if statement.

if ((d = [NSDictionary dictionaryWithContentsOfFile:fs]) != nil) that one


David
     
davecom  (op)
Mac Elite
Join Date: Jan 2001
Location: New York
Status: Offline
Reply With Quote
Jul 28, 2001, 04:09 PM
 
I got it! Well actually Andrew Abern got it and explained it to me. If anybody wants a free license code to URL Box that helped out email me.

I want to thank EVERYBODY for their interest and help. It ended up just being the [mt release]; statement. mt contains arrays that shouldn't have been released.

Imagine that! It was just a dumb error.

Thanks again,
David
     
davecom  (op)
Mac Elite
Join Date: Jan 2001
Location: New York
Status: Offline
Reply With Quote
Jul 28, 2001, 04:15 PM
 
I got it! Well actually Andrew Abern got it and explained it to me. If anybody wants a free license code to URL Box that helped out email me.

I want to thank EVERYBODY for their interest and help. It ended up just being the [mt release]; statement. mt contains arrays that shouldn't have been released.

Imagine that! It was just a dumb error.

Thanks again,
David
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:45 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2