我正在为我的分析代码编写单元测试.为了做到这一点,我正在编写一些函数来分配内存并调用'sleep'以生成可以测试的可预测内存和时间结果.
"分配内存"部分结果有点棘手.
没有'malloc'类型函数,我尝试追加一个字符串:$ myString.='x'; 并附加到数组:$ myArray [] = null;
但我的结果表明,这些结构不会按需线性分配内存.
您的建议表示赞赏!
这段代码是如何工作的???
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *addr = (int*) 0x4888d0;
*addr = 30;
printf("%i %p\n", *addr, addr);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我在一个表视图控制器的dealloc中遇到了EXC_BAD_ACCESS崩溃.释放给定retain属性的NSMutableArray时发生崩溃.我有第二个NSMutableArray也被赋予了一个retain属性,但它的发布不会导致崩溃.请查看以下代码,看看我是否忽略了有关内存管理的内容.谢谢.
在我的头文件中,我有以下内容:
@interface selectSourcesTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
NSMutableArray *selectedNames;
NSMutableArray *selectedAvailability;
}
@property (retain, nonatomic) NSMutableArray *selectedNames;
@property (retain, nonatomic) NSMutableArray *selectedAvailability;
Run Code Online (Sandbox Code Playgroud)
在我的实现中,我有以下内容:
@implementation selectSourcesTableViewController
@synthesize selectedNames;
@synthesize selectedAvailability;
- (void)viewDidLoad {
NSArray *names = [selectedSourceFileContent objectForKey:@"selectedNames"];
selectedNames = [[NSMutableArray alloc] initWithObjects: nil];
NSArray *availability = [selectedSourceFileContent objectForKey:@"selectedAvailability"];
selectedAvailability = [[NSMutableArray alloc] initWithObjects: nil];
for (int i=0; i < [names count]; i++) {
NSString *aName = [names objectAtIndex:i];
[selectedNames addObject: aName];
NSString *anAvailability = [availability objectAtIndex:i];
[selectedAvailability addObject: …Run Code Online (Sandbox Code Playgroud) 我应该NSBundle在下面的代码中发布吗?NSURL还应该发布还是不发布?我很迷惑.
NSBundle *mainBundle = [NSBundle mainBundle];
NSError *error;
NSURL *audioURL = [NSURL fileURLWithPath:[mainBundle pathForResource:@"count_in" ofType: @"mp3"]];
AVAudioPlayer *player1 = [(AVAudioPlayer*) [AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];
self.player = player1;
[self.player play];
[player1 release];
Run Code Online (Sandbox Code Playgroud) -(NSString *)returnString
{
NSString *str=[NSString new];
return str;
}
-(void)getString {
NSString *string=[self returnString];
[string release];
}
Run Code Online (Sandbox Code Playgroud)
这是一种适当/正确的释放方式NSString吗?
此外,如果自动释放对象的生命周期在runloop结束时为up.那么我们可以手动排空系统生成的自动释放池吗?这样我就可以释放所有自动释放的对象,我得到一个内存警告.
我用这个代码.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
view = [[UIView alloc] init];
[_window addSubview:view];
[view release];
NSLog(@"count - %d", [view retainCount]);
[self.window makeKeyAndVisible];
return YES;
}
- (IBAction)click{
NSLog(@"count - %d", [view retainCount]);
}
Run Code Online (Sandbox Code Playgroud)
当我点击uibutton时 - 我的视图保留计数= 2.为什么会发生这种情况?
例如,我有下一个代码:
#include <set>
using namespace std;
struct SomeStruct
{
int a;
};
int main ()
{
set<SomeStruct *> *some_cont = new set<SomeStruct *>;
set<SomeStruct *>::iterator it;
SomeStruct *tmp;
for (int i = 0 ; i < 1000; i ++)
{
tmp = new SomeStruct;
tmp->a = i;
some_cont->insert(tmp);
}
for (it = some_cont->begin(); it != some_cont->end(); it ++)
{
delete (*it);
}
some_cont->clear(); // <<<<THIS LINE
delete some_cont;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在删除some_cont以避免内存泄漏或自动调用析构函数之前,是否需要调用"THIS LINE"?
我目前正在开发一个类似"Dropbox"的应用程序,它有一个集成了文件/图像的查看器.
问题:当用户在服务器上存储了一个非常高分辨率的图像并想要在设备上查看它时,应用程序将崩溃.
示例:12000x4000像素的全景照片.有没有办法缩小这个图像(在客户端 - iOS - 侧)?
目前我正在使用这些课程:http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/
但我需要找到一种不使用> 300mb内存的方法......
这是一个哲学问题.我的程序发送需要更多内存的消息.我释放所有内存,以便继续正常工作.但是,该程序告诉我需要更多的内存,我没有人发布.我现在应该做什么?
这种情况几乎发生在我使用带有ZBar库的相机时,所以我无法控制ZBar(我不能释放它的内存,只有我的).这确实需要大量的内存才能工作.因为这个,我不知道我在做什么.
该程序没有崩溃,但可以运行不稳定.
我创建了一组相同的自定义类型的对象.所有对象都具有方法showDeleteButton了hideDeleteButton.我发现当我隐藏删除按钮(删除它)按钮时,按下了retainCounter == 2.
这里的代码:
-(void)showDeleteButton {
if(!isDeleteButtonLoaded) { // Check that method was't triggered twice
UIButton *aDeleteButton = [[UIButton alloc] initWithFrame:CGRectMake(-3, -7, 30, 29)]; // RC == 1
[aDeleteButton setImage:[UIImage imageNamed:@"close_button.png"] forState:UIControlStateNormal];
[self addSubview:aDeleteButton]; // RC == 2
[aDeleteButton addTarget:self action:@selector(deleteButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
deleteButton = aDeleteButton;
[aDeleteButton release]; // RC == 1
isDeleteButtonLoaded = YES;
NSLog(@"delete button retain count (created): %d", [deleteButton retainCount]);
[self setNeedsDisplay];
}
Run Code Online (Sandbox Code Playgroud)
}
-(void)deleteButtonPressed:(id)sender {
[delegate deleteImageAtPath:self.imageFullPath];
Run Code Online (Sandbox Code Playgroud)
}
-(void)hideDeleteButton {
if(isDeleteButtonLoaded) {
NSLog(@"delete …Run Code Online (Sandbox Code Playgroud) ios ×6
objective-c ×4
memory ×3
iphone ×2
memory-leaks ×2
retaincount ×2
c ×1
c++ ×1
image ×1
ipad ×1
php ×1
profiling ×1
resize ×1
retain ×1
stl ×1
unit-testing ×1