Results 1 to 2 of 2
Discuss Am I using Release properly? at the iPhone Developer Exchange - Hackint0sh.org; I am pretty new to Objective C, and instead code C# as day job. iPhone ...
  1. #1
    Newbie Array

    Join Date
    Mar 2008
    Posts
    2
    Post Thanks / Like
    Downloads
    0
    Uploads
    0
    Rep Power
    0

    Default Am I using Release properly?

    I am pretty new to Objective C, and instead code C# as day job. iPhone SDK brought me to the Mac coding world.

    Even though my ultimate goal is to write an iPhone application, I want to write my code in TDD (Test Driven Deployment) approach. Because OCUnit doesn't seem to work too well with iPhone SDK beta 3 (I wonder if I have better luck with beta 4 which released today and I already installed it), so I created a regular Cocca application, and then use OCUnit with it. My goal is to put all my logic in domain objects, have them all fully tested by OCUnit unit tests. And then eventually use all the objects in the iPhone app. In fact, I think this approach matches the philosophy of MVC design pattern, which the domain model objects should be decoupled from the UI (view) and controller (plumping or glue). Am I on the right track?

    So I wrote my unit tests, and one of them looks like this:

    Code:
    - (void) testChordWithInvalidChordName_ShouldNot_BeInstantiated
    {
    	NSString* chordNameToTest;
    	DeecayChord* newChordToTest;
    	BOOL isExceptionThrow;
    	
    	@try
    	{
    		chordNameToTest = @"SS";
    		newChordToTest = [[DeecayChord alloc] initWith: chordNameToTest];
    		
    		
    		isExceptionThrow = NO;
    	}
    	@catch (NSException *e)
    	{
    		isExceptionThrow = YES;
    	}
    	
    	STAssertTrue(isExceptionThrow, nil);
    	
    	//[chordNameToTest release];
    	//[newChordToTest release];
    
    	@try
    	{
    		chordNameToTest = @"Eb";
    		newChordToTest = [[DeecayChord alloc] initWith: chordNameToTest];
    
    		isExceptionThrow = NO;
    	}
    	@catch (NSException *e)
    	{
    		isExceptionThrow = YES;
    	}	
    	
    	STAssertFalse(isExceptionThrow, nil);
    
    	[chordNameToTest release];
    	[newChordToTest release];	
    }
    The code ran OK. You might notice that I remarked out the two release statement in the middle. If I unremarked then the code will failed, which got me slightly puzzled. Maybe I don't quit understand how alloc and init works. Here's my logic: when alloc got called, some new memory space was set aside for the object I instantiate, and that's why I need to release the memory I occupied at the end. But since the DeecayChord object got allocated twice for two different objects, I probably need to release each instance once, and thus calls release twice. But instead the code didn't compile if I do so.

    Obvious some assumptions I made was wrong, but should someone point out my logic flaw?

    Thanks!


  2. #2
    Professional Array

    Join Date
    Sep 2007
    Posts
    75
    Post Thanks / Like
    Downloads
    0
    Uploads
    0
    Rep Power
    9

    Default

    There's a good document on apple's site about retain/release usage. I recommend reading it.

    //[chordNameToTest release];
    Look at the usage for this variable. You never allocate storage, so you should not release this. Assignment of a static string (the @"blah blah" reference) is not the same as an alloc.

    [chordNameToTest release];
    These should work. You didn't have any trouble with them, right?

 

 

Similar Threads

  1. OS won't install properly!!!
    By kiko2k2kris in forum Installation
    Replies: 0
    Last Post: 12-29-2009, 11:27 AM
  2. Is that working properly ?
    By cheeqo in forum Hardware
    Replies: 1
    Last Post: 04-13-2009, 04:02 PM
  3. updating to 1.1.4 properly?
    By acidreian in forum iPhone "2G" (Rev. 1)
    Replies: 9
    Last Post: 03-15-2008, 11:01 PM
  4. Replies: 2
    Last Post: 02-23-2008, 02:08 AM
  5. ssh not working properly? help!
    By aiongiant in forum iPhone "2G" (Rev. 1)
    Replies: 2
    Last Post: 10-27-2007, 10:29 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Powered by vBulletin®
Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.
Search Engine Friendly URLs by vBSEO
(c) 2006-2012 Hackint0sh.org
All times are GMT +2. The time now is 05:03 AM.
twitter, follow us!