Solved

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

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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…
Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

770 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