我有一个简单的CoreData应用程序,它允许您将项目添加到列表中,显示在表格视图中.当用户键入新项时,将调用以下方法:
- (void)addNewItem:(NSString *)item
{
// Create a new instance of the entity managed by the fetched results controller.
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
Item *newItem = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
[newItem setName:item];
// Save the context.
NSError *error = nil;
if (![context save:&error])
{
//error handling code
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[context release];
[entity release];
[newItem release];
Run Code Online (Sandbox Code Playgroud)
该应用程序始终允许您将一个项目添加到列表中,但如果您尝试添加一个项目,则会崩溃.如果我删除"[newItem release];",该应用程序将允许您添加4个列表项,然后当您尝试输入第五个时突然崩溃.
只有删除方法末尾的所有三个发布语句时,应用程序才能正常工作.有谁能解释为什么?
我正在跟踪我的应用程序中的分配,并在分配数组时出现了非常奇怪的行为.这是我的代码的简单演示 -
public class Main extends Activity implements OnClickListener {
private Object[][] m = new Object[30000][];
@Override
public void onCreate(Bundle savedInstance) {
setContentView(R.layout.main);
super.onCreate(savedInstance);
findViewById(R.id.Button01).setOnClickListener(this);
}
@Override
public void onClick(View v) {
for (int i = 0 ; i < 30000 ; i++) {
// case 1
m[i] = new short[20];
// case 2
m[i] = new short[2][10];
}
}
}
Run Code Online (Sandbox Code Playgroud)
我运行了两个测试用例,其中我评论了案例行.在案例1中,当我分配一维数组时,一切似乎都是正常的
我正在与Daniel H Setinberg的"Cocoa Programming"开始我的Objective-C之旅.有一点让我对内存管理感到惊讶.实际上我发现Objective C中的内存管理比C更复杂,虽然我还没有触及"非垃圾收集语言"一段时间,所以使用malloc及其朋友的旧时代可能在我的记忆中理想化:).
让我困惑的是以下几点:
-(void) loadURLFromTextField{
NSURL *url = [NSURL URLWithString:self.address.text];
NSURLRequest *request = [NSURLRequest requestWithUrl:url];
[self.webView loadRequest:request];
}
Run Code Online (Sandbox Code Playgroud)
在第二行和第三行我分配了两个对象,所以我假设我需要在某处释放它们.然而,这段代码的评论指出:
"请注意,我们正在使用类方法来构建请求和URL的自动释放实例.我们不需要自己发布它们."
有人可以帮助我理解为什么这些实例是自动释放的,以及如何从SDK文档中获取此实例.返回对象实例的所有类方法实际上都是自动释放的标准吗?感谢您的帮助!
好的,所以我有一个只使用C结构和指针构建的二进制搜索树,因为我疯了,不想使用C++.无论如何,我有一些严重的内存泄漏,因为我假设 free(tree),树是下面结构的一个实例,并没有释放那棵树的所有孩子.
这是我的节点:
struct node{
struct node* parent;
struct node* left;
struct node* right;
int key; //the value of the node
};
Run Code Online (Sandbox Code Playgroud)
这是我的bst:
struct bst{
struct node* root;
int elements; //number of nodes in the bst
};
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,有没有比递归调用删除函数更好的方法呢?例如(在现场写这篇文章):
void delete_tree(struct node* n){
if(n == NULL) return;
struct node* left = n->left;
struct node* right = n->right;
free(n);
delete_tree(left);
delete_tree(right);
}
Run Code Online (Sandbox Code Playgroud) 我现在正在编写用于图像处理的小应用程序.但是我的程序内存使用存在很大问题.我是c ++的新手,以前我主要用c#编程.
几乎所有工作的功能都是这样的
while(!prototypeVector[i]->GetIsConvergaed())
{
if(previousPrototype!=NULL) delete previousPrototype;
previousPrototype = prototypeVector[i]->CopyPrototype();
if(&matchingPointVector!=NULL) matchingPointVector.clear();
matchingPointVector = prototypeVector[i]->CalculateMatchingPointsAll(imageDataVector);
distanseMatrix = CalculateDistanceAll(i);
membershipMatrix = UpdateMembershipAll(i);
if(translation)
{
tmatrix = UpdateTranslationMatrix(i);
if(directUpdate) prototypeVector[i]->SetTranslationMatrix( tmatrix);
//prototypeVector[i]->GetTranslationMatrix().DisplayMatrix();
tmatrix.DisplayMatrix();
}
if(scaling)
{
smatrix = UpdateScalingMatrix(i);
if(directUpdate) prototypeVector[i]->SetScalingMatrix(smatrix);
smatrix.DisplayMatrix();
}
if(rotation)
{
angle = UpdateAngleCoefficient(i);
cout<<endl;
Convert::RadiansToDegrees(angle)<<endl;
if(directUpdate)prototypeVector[i]->UpdateRotationMatrix(angle);
}
prototypeVector[i]->TransformTemplateOne(prototypeVector[i]->GetRotationMatrix(), prototypeVector[i]->GetScalingMatrix() , prototypeVector[i]->GetTranslationMatrix());
}
Run Code Online (Sandbox Code Playgroud)
我注意到如果在上面写的函数被称为另一个函数
CalculateMatchingPointsAll或CalculateDistanceAll或UpdateScalingMatrix内存使用率大幅上升(执行上述每个函数后300kB).所以我认为问题出在这些功能上.他们看起来像那样
vector<Point*> TemplateClusterPoint::CalculateMatchingPointsAll( vector<Point*> imageDataVector)
{
vector<Point*> matchinPointVector = vector<Point*>(imageDataVector.size(),new Point(0,0));
double minValue = DOUBLE_MAX_VALUE;
double currentDistance = 0;
for (int i=0;i<imageDataVector.size();i++ ) …Run Code Online (Sandbox Code Playgroud) 有人可以在mac计算机上然后在iphone设备上向我解释这个程序的输出.
我创建了一个不含任何东西的普通Foo类,Foo.h:
#import <Foundation/Foundation.h>
@interface Foo : NSObject {
}
@end
Run Code Online (Sandbox Code Playgroud)
和Foo.m:
#import "Foo.h"
@implementation Foo
@end
Run Code Online (Sandbox Code Playgroud)
要在Mac上测试它,我使用这个main.m:
#import "Foo.h"
int main(int argc, const char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Foo *myFoo;
[myFoo description];
printf("%p\n", myFoo);
[myFoo release];
[pool drain];
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个程序输出:
0x0
Run Code Online (Sandbox Code Playgroud)
但是在iphone上它会直接崩溃.
谢谢.
所述sizeof(test)返回24.是3点数组char*的指针,它们分别我的机器上的8个字节.我的问题是C是否也为角色正确分配了空间?我想这样做是因为这有效,但我想确保我正确地做到了.
char* test[] = {"QW", "BT", "GH"};
int size = sizeof(test) / sizeof(char*);
Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,用户可以在其中选择要显示位置的特定主题。我将选定的主题objectID存储在NSUserDefaults中,以便在应用程序重新启动时,选定的主题仍将保持不变。
在整个应用程序中,我会参考此选定主题。我目前有一个用于保存objectID的类变量,但是有时我需要检索对象本身的属性(即name属性)。为此,我从核心数据中检索对象,然后访问我的值。
我担心这样做所需的处理时间,所以我的问题是-更好的选择是什么?
1)每次需要访问属性时,将objectID存储在内存中并查询核心数据以检索对象。我认为这在内存使用量较少的情况下比较便宜,但是在处理方面却比较昂贵。
2)将actaul对象存储在内存中,然后在需要时直接访问该对象。我认为这在处理方面更便宜,但在内存使用方面可能会更昂贵。
我有一个问题,我需要用一些构造函数初始化一些对象的数组.让我说明我的意思:
ofstream* out = new ofstream[10];
for(int i = 0; i < 10; i++){
stringstream ss;
ss << "file" << i << ".txt";
string str(ss.str());
char *fileName = (char*)str.c_str();
out[i] = ofstream(fileName); //Now, this is wrong
}
Run Code Online (Sandbox Code Playgroud)
我需要在wrong标记线上提供一些帮助.如何分配该阵列的每个成员?
谢谢你没有把我指向其他帖子(我在帖子之前看了很多)
我有一个属性定义为:
@property(nonatomic, retain) UITableView *settingsTableView;
Run Code Online (Sandbox Code Playgroud)
然后在我的viewDidLoad方法中我有:
self.settingsTableView = [[[UITableView alloc] initWithFrame:tableFrame style:UITableViewStyleGrouped] autorelease];
[self.view addSubview:self.settingsTableView];
[self.settingsTableView release];
Run Code Online (Sandbox Code Playgroud)
然后在视图控制器的dealloc方法中我有:
[settingsTableView release];
Run Code Online (Sandbox Code Playgroud)
当我尝试从dealloc中释放时,我收到一条"消息发送到deallocated instance".我开始猜测自己,有人看到我做过什么愚蠢的事吗?
真的很感激这一个的帮助!