我在我的*.h文件中声明我的数组:
@interface aViewController: UIViewController
{
NSMutableArray *anArray; // You will need to later change this many times.
}
@end
我为它的*.m文件分配内存:
-(void) viewDidLoad
{
anArray = [[NSMutableArray alloc] init];
}
我单击一个测试按钮来加载我的数组(最终它需要在每次点击时加载DIFFERNT值):
anArray = [NSMutableArray arrayWithObjects:@"one", @"two", @"three", nil];
我在这里释放它:
-(void) dealloc
{
[anArray release];
[super dealloc];
}
这一切看起来都不错吗?
因为我以后运行此代码时崩溃:
NSLog(@"%d", [anArray count]);
不知道为什么一个简单的"NSLog()和count"会使一切崩溃.
短剑,
让我这样说吧:我对指针,数组,字符串和内存有很大的误解.
我已经阅读了我能在其上找到的所有内容......但是(还)要找到一个简单,清晰,易于理解的描述.
你能建议吗?(希望小于10页的阅读.)是否有解释的参考JUST这个话题:"你有12年以上的编码经验有史以来处理分配内存或指针......但没有" ......从的角度来看. )
所以变量名不是我引用对象的方式吗?那为什么呢?
我已经习惯了许多其他语言:
myString = "this" myString = "that" myInt = 5 myInt = 15
(什么可以更简单.)
在我看来这样做是最简单的方法.(而且它似乎有效.但这是真的正确吗?) …
我在现有类上有一个类别,它为类添加了一个属性和一些方法.
@interface AClass (ACategory) {
NSString *aProperty;
}
@property (nonatomic, retain) NSString *aProperty;
@end
Run Code Online (Sandbox Code Playgroud)
在实现文件中,我想在取消分配对象时释放此属性.但是,如果我dealloc在这个类中声明,它将根据我的理解覆盖原始类的dealloc.aProperty当对象被释放时,释放它的正确方法是什么?
@implementation AClass (ACategory)
@synthesize aProperty;
- (void)dealloc {
[aProperty release];
// this will skip the original dealloc method from what I understand
[super dealloc];
}
@end
Run Code Online (Sandbox Code Playgroud) 我知道很少需要覆盖alloc或dealloc方法,但如果需要,可以在iPhone编程中使用吗?
我有以下代码使用std :: list容器测试内存释放:
#include <iostream>
#include <list>
#include <string>
#include <boost/bind.hpp>
/* count of element to put into container
*/
static const unsigned long SIZE = 50000000;
/* element use for test
*/
class Element
{
public:
Element()
: mId(0)
{}
Element( long id )
: mId(id)
{}
virtual ~Element()
{
}
inline long getId() const
{
return this->mId;
}
inline bool operator<( const Element & rightOperand ) const
{
return this->mId < rightOperand.mId;
}
inline bool isEven() const
{ …Run Code Online (Sandbox Code Playgroud) 我对iOS开发相对较新,所以感谢您帮助找出我遇到的错误的根本原因.
我尝试使用Instruments(即Allocations-Zombie Profiler)调试错误,但我无法理解所呈现的调用堆栈.
这是用户界面链接的方式:TabBarController - > NavigationController - > TopPlacesTableViewController - > RecentPhotosTableViewController - > PhotoViewController
单击最后一个视图中的"后退"按钮(即PhotoViewController的后退按钮)时发生错误.此操作应该显示以前的RecentPhotosTableViewController,但是在viewWillAppear和ViewDidAppear的事件之间的某个时间访问了未知的解除分配的对象.
另外,我有一个GenericTableViewController,它是TopPlacesTableViewController和RecentPhotosTableViewController的父级.子项在父项中设置NSMutableArray属性,该属性是在子视图中加载的数据.
我目前正在使用iOS6和XCode4.5.
[更新:在控制台中,此行显示 - "[UIView _forgetDependentConstraint:]:发送到解除分配的实例xxx的消息"].
在客户端手机上,相机释放期间很少会发生崩溃
Fatal Exception: NSRangeException
Cannot remove an observer <AVCaptureSession 0x174212170> for the key path "changeSeed" from <AVCaptureConnection 0x17420fa60> because it is not registered as an observer.
Thread : Fatal Exception: NSRangeException
0 CoreFoundation 0x000000018449259c __exceptionPreprocess + 132
1 libobjc.A.dylib 0x0000000194be40e4 objc_exception_throw + 60
2 CoreFoundation 0x00000001844924dc -[NSException initWithCoder:]
3 Foundation 0x00000001852a7e9c -[NSObject(NSKeyValueObserverRegistration) _removeObserver:forProperty:] + 528
4 Foundation 0x00000001852a7954 -[NSObject(NSKeyValueObserverRegistration) removeObserver:forKeyPath:] + 104
5 AVFoundation 0x0000000182d21054 -[AVCaptureSession _removeConnection:] + 192
6 AVFoundation 0x0000000182d206dc -[AVCaptureSession _removeVideoPreviewLayer:] + 120
7 AVFoundation 0x0000000182d300f8 -[AVCaptureVideoPreviewLayer …Run Code Online (Sandbox Code Playgroud) 我有一个问题,似乎是基于ARC的应用程序中的使用中对象的过早发布.我正在尝试在FTP服务器上创建一个文件夹.代码的相关部分如下; 我先说一下这个问题.
代码的问题是,调试输出中的
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
Run Code Online (Sandbox Code Playgroud)
方法永远不会被调用.
相反,我只是得到一个_EXC_BAD_ACCESS_错误.调试时我发现了两件事:
只有在执行以下代码行(createDir方法)时才会出现错误:
[ftpStream open];
Run Code Online (Sandbox Code Playgroud)如果没有发送该消息,其余代码实际上没有意义 - 但它也不会崩溃......
我使用NSZombieEnabled跟踪EXC_BAD_ACCESS:启用僵尸对象后,GDB会生成以下调试器信息:
*** -[FTPUploads respondsToSelector:]: message sent to deallocated instance 0x9166590
Run Code Online (Sandbox Code Playgroud)引用的地址0x9166590是我的FTPUploads对象的地址.在委托处理消息之前,它似乎已被释放.
为什么系统会释放一个正在使用的对象?我怎样才能防止它过早解除分配?
码:
FTPUploads.h摘录:
#import <Foundation/Foundation.h>
enum UploadMode {
UploadModeCreateDir,
UploadModeUploadeData
};
@class UploadDatasetVC;
@interface FTPUploads : NSObject<NSStreamDelegate> {
@private
NSString *uploadDir;
NSString *ftpUser;
NSString *ftpPass;
NSString *datasetDir;
NSArray *files;
/* FTP Upload fields */
NSInputStream *fileStream;
NSOutputStream *ftpStream;
// some more fields...
enum UploadMode uploadMode;
UploadDatasetVC *callback;
}
- (id) initWithTimeseriesID: (int) …Run Code Online (Sandbox Code Playgroud) 在iOS中,我从当前的viewController弹出到前一个,但它不会进入dealloc.
这是因为还有另一个指向当前viewController的指针,在不同的viewController中还是在当前的viewController中?
这是我弹出前一个视图的地方:
- (IBAction)fileUploadCancelTouched:(UIButton *)sender {
[self.fileToUpload cancel];
[self.view hideToastActivity];
[self.greenprogressBar removeFromSuperview];
[self.subView removeFromSuperview];
self.fileUploadCancelButton.hidden = YES;
if (self.commandComeBackToFinalScreen == 1) {
[self.navigationController popViewControllerAnimated:YES];
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的dealloc功能:
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
self.greenprogressBar = nil;
self.fileUploadCancelButton = nil;
self.fileToUpload = nil;
[buttonHome_ release];
[buttonTestMeAgain_ release];
[buttonMarkMyTest_ release];
[examId_ release];
[sender_ release];
self.ob = nil;
[_fileUploadCancelButton release];
[super dealloc];
}
Run Code Online (Sandbox Code Playgroud) 我一直在努力解决这个问题,因为某些原因我的SKScenes没有正确解除分配,这导致内存增长有限,因为每次我退出并进入场景时内存会跳起来.这意味着在玩了10轮比赛之后App崩溃了.据我所知,经过多次检查后,我没有任何保留周期或强烈引用场景本身,虽然我知道纹理被缓存并保存在内存中,一旦预加载,内存不应该每次都上升.
这就是我在viewcontroller中设置skview和第一个场景的方法:
-(void)loadStartScreen{
SKView *theView = (SKView *) self.view;
theView.showsFPS = YES;
theView.showsNodeCount = YES;
//Sprite Kit applies additional optimizations to improve rendering performance
theView.ignoresSiblingOrder = YES;
// Create and configure the scene.
MainMenuScene *theScene = [MainMenuScene sceneWithSize:theView.bounds.size];
theScene.scaleMode = SKSceneScaleModeAspectFill;
theScene.backgroundColor = [UIColor grayColor];
// Present the scene
[theView presentScene:theScene];
Run Code Online (Sandbox Code Playgroud)
这是我的MainMenuScene中的代码,用于创建并移动到下一个场景:
SKScene *theScene;
SKTransition *theTransition;
switch (theTag.intValue) {
case 0: // start game
// stop music
[[appDelegate musicPlayer]stop];
theScene = [[GameLevelScene alloc] initWithSize:self.view.bounds.size];
// transition type
theTransition = [SKTransition …Run Code Online (Sandbox Code Playgroud) 我一直在努力解决这个问题,但是我无法找到解决方案:
我有一个支持所有设备系列的iOS 9应用程序,使用大小类并使用Swift 2.0进行编程.我正在使用a UISplitViewController,一切都按照我的要求工作,除了在崩溃的环境中(例如在iPhone上).
Master-ViewController是在UITableViewController选择单元格时触发替换segue的.在折叠环境中,这意味着detailViewcontroller被推送到屏幕上.在UISplitViewController视觉上表现有点像UINavigationController.但是,当我使用后退按钮或滑动手势关闭detailViewController时,在Master-ViewController中触发新的替换segue之前,它不会被释放.
我认为这是一个特征UISplitViewController,因为它最初设计为显示彼此相邻的两个内容.然而,在崩溃的环境中,我希望我UISplitViewController的行为像一个简单的UINavigationController,在弹出时释放先前推送的detailviewController.
viewControllers在弹出detailViewController后,我一直在尝试手动更改splitViewController的属性:
if let firstVc = self.splitViewController?.viewControllers.first {
self.splitViewController?.viewControllers = [firstVc]
}
Run Code Online (Sandbox Code Playgroud)
但这没有用.简单地用一个空的"Dummy"-ViewController替换detailViewController也不起作用,因为它会自动动画转换.和UISplitViewControllerDelegate我一起玩并没有帮助我...
有没有解决方案(也许简单?:)),我太盲目了?
dealloc ×10
ios ×5
objective-c ×5
crash ×2
alloc ×1
c++ ×1
deinit ×1
iphone ×1
lifecycle ×1
nszombie ×1
overriding ×1
skscene ×1
sprite-kit ×1
stl ×1
xcode ×1