alloc

Why are alloc and init called separately in Objective-C?

Note: I'm relatively new to Objective-C and am coming from Java and PHP. Could someone explain to me why I always have to first allocate and then initialize an instance? Couldn't this be done in the init methods like this: + (MyClass*)init { MyClass *instance = [MyClass alloc]; [instance setFoo:@"bla"]; return instance; }...

Memory allocation, release and NSURLConnection in iPhone app

I'm hoping someone can help me with this. I'm struggling to find an answer to what should be an easy question. By the way, this is my first major obj-c project after years of using c and c# so feel free to point out things I'm failing on. I have an iPhone app designed to load an album of photos into a UIScrollView. It also has a rand...

NSButton state != 0 when alloc'd

I have two projects that include the same code: NSButton *button = [[NSButton alloc] initWithFrame:ctrlRect]; (gdb) print (int)[button state] $1 = 1 in the other project $1 = 0 I am compiling both with GCC 4.2, 10.6 - no other apparent differences in compiler settings. If I alloc/init an NSButton earlier in the app, state = 0 wh...

What objects do I alloc/release

Hi, in Objective-C and simply speaking - am I correct when assuming that all pointer variables must be released when they're not used any more? Every pointer variable (*) is a class of some kind, or not? Every pointer variable therefore needs to be allocated and initialised using "alloc" and "init" (or similar)? When declaring variale...

template style matrix implementation in c

From time to time I use the following code for generating a matrix style datastructure typedef double myType; typedef struct matrix_t{ |Compilation started at Mon Apr 5 02:24:15 myType **matrix; | size_t x;...

How to debug anomalous C memory/stack problems

Hello, Sorry I can't be specific with code, but the problems I am seeing are anomalous. Character string values seem to be getting changed depending on other, unrelated code. For example, the value of the argument that is passed around below will change merely depending on if I comment out one or two of the fprintf() calls! By the la...

Tracking EXC_BAD_ACCESS on iPad

I've been using this code to create my UIWindow UIMyWindow* win = [[UIMyWindow alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; UIMyWindow isn't anything special it just has a pointer to a C++ class that does some wrapping of ObjectiveC. Recently my application start crashing after adding some line ...

obj-c NSString and alloc / retain / release

This is probably a question that is more about object alloc/retain/release, but I'll use NSString as an example. I'm aware that I can do: NSString* myString = [[NSString alloc] initWithString:@"Test"]; to essentially allocate and initialize a string referenced by my variable myString which I should later call [myString release] upon....

iPhone Memory allocation problems

Within my application I have 20 or so ViewControllers and Xibs that the user should be able to access. The problem is after looking at 5 or so of them the application crashes due to lack of memory. I have released all memory that I have allocated within the ViewControllers so I can only assume it's because its holding the memory of so ma...

iPhone image problems.

I'm having frequent crashes on iOS 4.0 within my application due to excessive memory. Instruments tells me that it's category is "Malloc 600KB" and the responsible library is ImageIO and the responsible caller is ImageIO_Malloc. I'm using lots of images in this app but no more than I have used in other applications. It's odd because if I...

Calling singleton object initiated from nib.

Hi, I have an object which is initiated in my nib file. I want it to be a singleton but also accessible from code through [myClass sharedInstance];. Right now I have this: static myClass *singleton = nil; @implementation myClass + (myClass *)sharedInstance { if (!singleton) singleton = [[self class] new]; return singleton; } ...

How can I get an NSNumber without performing alloc on it, so it will respond to initWithInt?

My understanding is that a 'convenience' method such as [nsnumber initWithInt] should create a copy of the indicated class, initialized to the desired value. minutesLeft=[NSNumber initWithInt:((timeLeft)%60)]; Timeleft is an integer, so initWithInt should be working, and the result should be that minutesLeft (a property set to retain)...

Random malloc crashes in stringByReplacingOccurrencesOfString

Hi, I am getting random malloc crashes in stringByReplacingOccurrencesOfString. I've noticed that it crashes on longer strings, but I can't seem to find why. What could be the problem? The error: CashTrader(53448,0xb0103000) malloc: *** error for object 0x5c5eca0: incorrect checksum for freed object - object was probably modified afte...

Objective C: [MyObject alloc] now crashes under iOS SDK 4.1

I'm working on an existing, large-ish codebase, and after upgrading the iOS SDK to 4.1 I am now seeing very strange behaviour. The crux of the matter appears to be a particular class that will no longer alloc - it is throwing a bad access in obj_msgSend, and seems to be the Class object on the stack that objc_msgSend doesn't like - altho...

Does copy allocate the required memory ?

Hi, Here is an example taken from Apple iPhone SDK project: @interface EADSessionController : NSObject <EAAccessoryDelegate, NSStreamDelegate> { EAAccessory *_accessory; EASession *_session; NSString *_protocolString; NSMutableData *_writeData; NSMutableData *_readData; } ... // initialize the accessory with the ...