Solved

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

Posted on 2011-03-10
2
1,225 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In this article I'm going to cover the basics and usages of NSInvocation. What is NSInvocation? Apple Developer Reference describes NSInvocation this way: An NSInvocation is an Objective-C message rendered static, that is, it is an action tur…
iPad in the Business – Quick Start Part 1 Getting Started with Active Sync Mail Many people seem to have issues connecting their iOS device to their company Exchange Server and this article covers the steps for Active Sync configuration as wel…
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.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

728 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