Solved

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

Posted on 2011-03-10
2
1,226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 500 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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

CocoaPods is the best way to manage library dependencies in iOS and OS X projects. By using cocoa pods there is no need of downloading the code from github and copying to your project. There are plenty of open source libraries now available with C…
Learn about the eCommerce marketing trends for the year ahead.
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.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

623 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