Solved

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

The Basics of Storyboard Storyboards were first introduced to developers in the release of iOS 5. They save the developer the trouble of designing each screen’s interface in a different file. The storyboard allows you to see the conceptual mockup o…
     Intro      In this article, I will show you how to parse a JSON in Swift using an open source library called SwiftyJSON (https://github.com/SwiftyJSON/SwiftyJSON). If you haven't heard of a JSON before, it stands for JavaScript Object Notation…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
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.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now