Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

After repeatedly adding/removing a UIScrollView with a custom image, app crashes

Posted on 2011-03-10
2
Medium Priority
?
1,235 Views
Last Modified: 2012-05-11
The following code creates a scrollview and adds it to my main view. It also creates a UIImageView inside the ScrollView and also adds a tiny little image to the mainview in its own UIImageView. The class ScrollView is just UIScrollView but it also implements touch events. (My problem happens if I use the base class UIScrollView so I know that isn't the issue). The UIScrollView contains a custom image that I create at run-time from a bitmap context.

What's happening is this view will show up (it's just a white scrollable box within my view). And the three "removeFromSuperView's" at the end will remove them, as it should. The problem is, if I repeat this 12-18 times, the application will crash! I've been fighting this for over a day. Does anyone have any ideas? The code is for iOS 3.2 on an original iPad.

I *DID* notice that if I replace the [imview setImage:uimg] with a static image, the crashing never occurs. I can do this 200 times and never encounter a crash. That is, replacing it with [imview setImage:[UIImage imageNamed:@"blahblah.png"]]; will stop the crashing. But, I need to be synthesizing the image at run-time for obvious reasons.

Any ideas? :)

      
msview = [[ScrollView alloc] initWithFrame:CGRectMake(Mx, My, Mwid, Mhgt)];
	[myview addSubview:msview];
	msview.backgroundColor = [UIColor grayColor];
	closex = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"someimage.png"]];
	[myview addSubview:closex]; closex.frame = CGRectMake(Mx+Mwid-15, My-15, 30, 30);
	
	ulong *gbuff=(ulong*)malloc(TOThgt*Mwid*4);
	for(int i=0;i<(tot>>2);i++) gbuff[i]=0xfff0f0f0;
	CGColorSpaceRef colorSpace;
	colorSpace = CGColorSpaceCreateDeviceRGB();
	int bytewid = Mwid; bytewid*=4;
	CGContextRef bmCtx = CGBitmapContextCreate(gbuff, Mwid, TOThgt, 8, bytewid, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
	CGImageRef image = CGBitmapContextCreateImage(bmCtx);
	CFRelease(bmCtx); free(gbuff); CGColorSpaceRelease(colorSpace);
	UIImage *uimg = [UIImage imageWithCGImage:image]; CGImageRelease(image);
	imview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Mwid,TOThgt)];
	[imview setImage:uimg];
	[msview addSubview:imview];
	[msview setContentSize:CGSizeMake(Mwid, TOThgt)];

.
.
.

	[closex removeFromSuperview];
	[imview removeFromSuperview];
	[msview removeFromSuperview];

Open in new window

0
Comment
Question by:wllunn
2 Comments
 
LVL 3

Expert Comment

by:gawhemant
ID: 35107341
Hello,

You might be releasing somethng you should not be releasing through the method life cycle.It happens Since memory managment is the most cruicial part.

Hope this tip will help you do searching in better direction.

Regards,
Hemant
0
 
LVL 5

Accepted Solution

by:
mad_mac earned 2000 total points
ID: 35108211
it does sound like a memory release issue as Hemant suggests however i doubt it is in the life cycle methods given this seems to work without issue when a static image is used.

you could try using CGBitmapContextCreateWithData providing a call back which will be called when the context is release, you can then ensure that the allocated memory is only released when the context is destroyed.

Also have you tried running this in profile mode to see if there are any obvious memory leaks..?

And on a final point have you tried the code on a later version of iOS as a test..?  Possible this is a defect in one of the core graphics libs.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The release of the iPhone 6 and the iPhone 6 Plus was extremely successful for Apple. However when iOS 8.1 was introduced, many iPhone users complained of quick battery drainage. Without even knowing, you may be unnecessarily using up your battery p…
How to take pictures with depth using iOS 10
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
Integration Management Part 2
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question