Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-15-2011, 02:26 PM   #1 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 59
kend0g187 is on a distinguished road
Default random data loss?

I have a custom object, of which I have an array within another custom object, and I have an array of those in my App Delegate. Something like this:

SubObject : NSObject {
some properties...
}

MyObject : NSObject {
some properties...
NSMutableArray *subObjects
}

AppDelegate : NSObject {
NSMutableArray *myObjects
}

In my code I initialize the myObjects array with properties set on both the myObjects and the subObjects, everything working fine. Then, at some future point, during some code that has nothing to do with the myObjects array, the subObjects all of a sudden lose their data. What could cause this?
kend0g187 is offline   Reply With Quote
Old 01-15-2011, 06:23 PM   #2 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 59
kend0g187 is on a distinguished road
Default

Is there something special you have to do when you have two NSMutableArrays of custom objects nested within each other? I'm getting all kinds of data loss/overwriting. Plus, Instruments is telling me I have memory leaks even though I have all the right release calls. Anyone have a hint as to whats happening? Thanks.
kend0g187 is offline   Reply With Quote
Old 01-15-2011, 07:23 PM   #3 (permalink)
Use [code] tags please
 
Join Date: Jun 2009
Location: Jacksonville, FL
Posts: 410
timle8n1 is on a distinguished road
Default

Quote:
Originally Posted by kend0g187 View Post
Is there something special you have to do when you have two NSMutableArrays of custom objects nested within each other? I'm getting all kinds of data loss/overwriting. Plus, Instruments is telling me I have memory leaks even though I have all the right release calls. Anyone have a hint as to whats happening? Thanks.
You show no code and claim no leaks though apparent Instruments thinks differently. How is someone supposed to help you?
timle8n1 is offline   Reply With Quote
Old 01-15-2011, 11:02 PM   #4 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 59
kend0g187 is on a distinguished road
Default

Code:
@interface Tile : NSObject {
	BOOL pressed;
	int row;
	int column;
	int section;
	int value;
}

@interface Section : NSObject {
	BOOL solved;
	int sectionRow;
	int sectionCol;
	NSMutableArray *tiles;
}

for( int r = 1; r <= 9; r++ ) {
			for( int c = 1; c <= 9; c++ ) {
				Section *sec = [[Section alloc] init];
				sec.solved = NO;
				sec.sectionRow = r;
				sec.sectionCol = c;
				sec.tiles = [[[NSMutableArray alloc] initWithCapacity: 9] autorelease];
				for( int t = 1; t <= 9; t++ ) {
					Tile *aTile = [[Tile alloc] init];
					aTile.pressed = NO;
					aTile.value = t;
					aTile.row = (r-1)*3 + (t-1)/3 + 1;
					aTile.column = (c-1)*3 + (t-1)%3 + 1;
					[sec.tiles addObject: aTile];
					[aTile release];
				}
				[sections addObject: sec];
				[sec release];
			}
		}
Sorry, I wasn't sure if the code would help. But here it is.

Last edited by kend0g187; 01-15-2011 at 11:04 PM.
kend0g187 is offline   Reply With Quote
Old 01-16-2011, 12:39 AM   #5 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 59
kend0g187 is on a distinguished road
Default

I was able to fix my problem, by removing the release calls to "sec" and "aTile". Of course, now I have memory leaks. Can anyone shed some light on what's happening? Thanks.
kend0g187 is offline   Reply With Quote
Old 01-16-2011, 09:25 AM   #6 (permalink)
Use [code] tags please
 
Join Date: Jun 2009
Location: Jacksonville, FL
Posts: 410
timle8n1 is on a distinguished road
Default

Quote:
Originally Posted by kend0g187 View Post
I was able to fix my problem, by removing the release calls to "sec" and "aTile". Of course, now I have memory leaks. Can anyone shed some light on what's happening? Thanks.
What does the property line or setter look like for tiles?
timle8n1 is offline   Reply With Quote
Old 01-16-2011, 09:43 AM   #7 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

The code you posted looks correct, *assuming* that the "sections" array does not get leaked, that you've written a proper dealloc method for the Section class, and that "tiles" is a retain property. Removing those releases is not correct, as you figured out.

Whenever you see leaks make sure you tackle the collection classes and parent objects first. If you forget to release one array and it contains 50 strings, you'll see 51 leaks in the leaks instrument. When you fix the array leak they all go away.
__________________

Free Games!

Last edited by smasher; 01-16-2011 at 06:14 PM.
smasher is offline   Reply With Quote
Old 01-16-2011, 01:32 PM   #8 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 59
kend0g187 is on a distinguished road
Default

Thanks for the help guys. I think my properties are correct:

In Section.h I have:

@property (nonatomic, retain) NSMutableArray *tiles;

In Section.m I have:

- (void) dealloc {
[tiles release];
[super dealloc];
}

Elsewhere in my code, after the sections array is initialized, I have this:

Code:
Section *sec = [[Section alloc] init];
sec = [sections objectAtIndex: index];
...
[sec release];
Is that correct, or could that be part of the problem?
kend0g187 is offline   Reply With Quote
Old 01-16-2011, 01:45 PM   #9 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Quote:
Originally Posted by kend0g187 View Post
Elsewhere in my code, after the sections array is initialized, I have this:

Code:
Section *sec = [[Section alloc] init];
sec = [sections objectAtIndex: index];
...
[sec release];
Is that correct, or could that be part of the problem?
Yoinks! That's not correct. You're creating a new pointer, and alloc+init a new Section for it to point to.

Then you point the pointer (sec) at a different object, leaking one you just alloc'd.

Then you release the object that you got from the sections array, even though you never retained it. I am surprised that doesn't crash your program.

Code:
Section *sec =[sections objectAtIndex: index];
// do stuff with sec
// no need to release; we didn't retain it.
Your snippet indicates a confusion between pointers and objects. You can have two pointers to the same object, and you don't need to alloc a new object every time you have a new pointer. That confusion is probably the root of all of your issues.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 01-16-2011, 03:29 PM   #10 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 59
kend0g187 is on a distinguished road
Default

*

Last edited by kend0g187; 01-16-2011 at 04:03 PM. Reason: No longer applicable
kend0g187 is offline   Reply With Quote
Old 01-16-2011, 04:02 PM   #11 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 59
kend0g187 is on a distinguished road
Default

I'm seem to be getting it. I'm now down to just two memory leaks, with the program working. But I'm confused about one thing. I have this code:

Code:
- (id) copyWithZone: (NSZone *) zone {
	Section *sectionCopy = [[Section allocWithZone: zone] init];
	sectionCopy.solved = solved;
	sectionCopy.sectionRow = sectionRow;
	sectionCopy.sectionCol = sectionCol;
	sectionCopy.tiles = [[NSMutableArray alloc] init];
	for( Tile *aTile in tiles ) {
		Tile *tileCopy = [aTile copy];
		[sectionCopy.tiles addObject: tileCopy];
		[tileCopy release];
	}
	//[sectionCopy.tiles release];
	return sectionCopy;
}
The alloc-ing of the Mutable Array seems to be giving me a leak, but if I uncomment the release, my app crashes, and if I don't alloc the array then I can't add objects to it. What is the solution here?
kend0g187 is offline   Reply With Quote
Old 01-16-2011, 06:14 PM   #12 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

I'd write that line like this:
Code:
sectionCopy.tiles = [NSMutableArray array];
That creates an autoreleased array, but property will retain it. Now you don't need to call [sectionCopy.tiles release] here.

If you make this change and your app starts crashing then you are over-releasing "tiles" somewhere else.

Remember that the leaks tool tells you where the leaked object was created, *not* the line that is wrong, broken, or "causing" the leak.
__________________

Free Games!
smasher is offline   Reply With Quote
Reply

Bookmarks

Tags
data, loss

Thread Tools
Display Modes

Posting 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



» Advertisements
» Online Users: 365
10 members and 355 guests
7twenty7, blueorb, dre, iAppDeveloper, iGamesDev, Mah6447, Morrisone, mottdog, sacha1996, Touchmint
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:37 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0