我的程序中有一个非常大的循环,我使用了很多临时变量和实例变量.当我的循环继续运行时,程序会占用越来越多的内存,直到崩溃为止.在这种情况下,我可以就如何正确的内存管理获得一些建议吗?我的主要问题是,为什么以下代码有误?
以下是导致泄漏的代码:
(void)processTrackValues:(NSMutableArray*)tags {
NSImage*trackArt = [tags objectAtIndex:5];
NSMutableArray*tempArtArray = [[NSMutableArray alloc] init];
[tempArtArray addObject:trackArt];
[tempArtArray发布];
}
我也尝试过:
(void)processTrackValues:(NSMutableArray*)tags {
NSImage*trackArt = [tags objectAtIndex:5];
NSMutableArray*tempArtArray = [[NSMutableArray alloc] init];
[tempArtArray addObject:trackArt];
[trackArt发布];
[tempArtArray发布];
}
我正在尝试学习C++,在这个过程中我试图编写一个函数,它获取两个char指针并将第二个连接到第一个(我知道有strcat这个).
但是 - 我想要完成的是修改第一个参数指针,使其指向结果.因此我在第一个参数中使用了对指针的引用.
在从函数返回之前我想释放第一个参数内存,但是我收到一个错误.
这是代码:
void str_cat(char*& str1, char* str2)
{
if (!str1)
{
str1 = str2;
return;
}
if (!str2)
return;
char * new_data = new char[strlen(str1) + strlen(str2) +1];
char * new_data_index = new_data;
char * str1_index = str1;
char * str2_index = str2;
while(*str1_index)
*new_data_index++ = *str1_index++;
while(*str2_index)
*new_data_index++ = *str2_index++;
*new_data_index = NULL;
delete str1; //ERROR HERE (I also tried delete[] str1)
str1 = new_data;
}
Run Code Online (Sandbox Code Playgroud)
我不懂为什么.
有什么建议?
谢谢,
Itay \
编辑 以下是我如何使用该功能 …
我有一个奇怪的问题,但我很确定,因为你不是.如果是这样,请帮助我或解释.我有一个39 UIImages的数组,将用于动画UIImageView.播放动画后,内存不会释放.
- (void)viewDidLoad {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSMutableArray* actionArray = [[NSMutableArray alloc]initWithCapacity:38];
for (int i=1; i<=39; i++) {
NSString* path = [[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"bg_Level1_%.4d", i] ofType:@"png"];
UIImage *image = [[UIImage alloc]initWithContentsOfFile:path];
[path release];
[actionArray addObject:image];
[image release];
NSLog(@"path rc %d image %d", [path retainCount], [image retainCount]);
}
imageView.animationImages = actionArray;
imageView.animationDuration = 4.0;
imageView.animationRepeatCount = 1;
[imageView startAnimating];
NSLog(@"RetainCount ActArr %d ImageView %d", [actionArray retainCount], [imageView retainCount]);
[actionArray release];
[pool drain];
[imageView release];
[self performSelector:@selector(playAnimation2) withObject:self afterDelay:5.0]; …Run Code Online (Sandbox Code Playgroud) 有没有办法阻止std::set< Object*>.erase(it)函数删除内存?我只希望Object*从集合中删除,而不释放内存..
具体来说,我有一个对象将自己从集合中移除并将自己置于另一个集合中.它崩溃了.
set1.insert(this);
set1.erase(set1.find(this));
set2.insert(this);
Run Code Online (Sandbox Code Playgroud)
有人能够重现这个吗?谢谢!
我对检查对象的保留计数有一点疑问:
请找到下面的代码,它在释放对象内存后显示retainCount为1.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
CGRect contentRect = [cell.contentView bounds];
UIImageView *thumbView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:IMAGE_CELL_BACKGROUND]];
thumbView.frame = contentRect;
cell.backgroundView=thumbView;
[thumbView release];
}
UIImageView *image=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"left_arrow.png"]];
**NSLog(@"Image retain count %d",[image retainCount]);**
image.frame=CGRectMake(290, 12.5, [UIImage imageNamed:@"left_arrow.png"].size.width, [UIImage imageNamed:@"left_arrow.png"].size.height);
image.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:image];
[image release];
**NSLog(@"Image retain count-- after %d",[image retainCount]);**
// Configure the …Run Code Online (Sandbox Code Playgroud) 我试图在较低的层次上理解C如何管理内存。我在网页上找到了一些代码,其目的是教您如何使糟糕的内存管理变得糟糕—因此,我将其复制并粘贴并编译:
int main(int argc, char **argv) {
char *p, *q;
p = malloc(1024);
q = malloc(1024);
if (argc >= 2)
strcpy(p, argv[1]);
free(q);
free(p);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
用通用命令执行测试用例
/development/heapbug$ ./heapbug `perl -e 'print "A"x$K'`
Run Code Online (Sandbox Code Playgroud)
因为$K < 1023我没想到会有问题,但是因为我没想到$K = 1024会发生核心转储。长话短说,我开始为segfaults $K > 1033。
两个问题:1)为什么会这样?2)是否有一个公式可以说明系统的“容差”?
试图做一个简单的内存管理任务。我有一个名为 的类Vehicle,我试图重复将类 Vehicle 的一个实例分配给一个Vehicle *指针。但是,每当我尝试为指针赋值时,程序都会崩溃并显示以下输出:
munmap_chunk(): invalid pointer
Aborted (core dumped)
Run Code Online (Sandbox Code Playgroud)
我int main()的很简单:
int main(int argc, char **argv)
{
Vehicle * ptrVehicle;
int d, w;
char input;
while (1) {
cout << "Enter q to quit, otherwise create vehicle object." << endl;
cin >> input;
if (input == 'q' || input == 'Q') {
break;
} else {
cout << "Enter the number of doors > \t";
cin >> d;
cout << endl << …Run Code Online (Sandbox Code Playgroud) 因此,当我了解指针时,我得到了一些疯狂的想法:如果我打印一个指针,它会给出内存中的一个地址,如果是这样,那么实际上读取了计算机内存的哪一部分?例如
#include <iostream>
using namespace std;
main()
{
int a=1;
int* ptr=&a;
int i=0;
while(1)
{
cout<<(ptr+i)<<"\t"<<*(ptr+i)<<"\n";
i++;
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行这个程序时(考虑到没有错误),它给了我来自&a++ 的地址。因此它循环访问内存中的地址,并显示存储在那里的信息。所以我的问题是,正在读取内存的哪一部分?我是否可以访问每个内存位置(每个内存位置中存储的值)?我可以通过这样的方式删除我的计算机上的文件(这听起来很愚蠢)吗
ptr=nullptr;
while(1)
{
*(ptr+i)=0;
i++;
}
Run Code Online (Sandbox Code Playgroud)
我不知道我的语法是否正确,尽管你明白了。我不敢在我的电脑上运行这个...
我希望这里不要听起来很愚蠢,但是 NULL 模块在执行此操作时实际上是否需要内存分配:
TheNull = malloc(sizeof(NULL));
Run Code Online (Sandbox Code Playgroud)
如果是真的,那么没有分配内存的东西怎么可能存在于内存中呢?
fn test() -> *const Vec<u8> {
let b = vec![9_u8];
let ret: *const Vec<u8> = &b;
println!("ret ptr={:#p} inside {:#p}", ret, b.as_ptr());
std::mem::forget(b);
ret
}
fn main() {
let a = test();
let v = unsafe {
&*a
};
println!("ret ptr={:#p} inside {:#p} value={}", v, v.as_ptr(), v[0]);
println!("ret ptr={:#p} inside {:#p} value={}", v, v.as_ptr(), v[0]);
}
Run Code Online (Sandbox Code Playgroud)
我的机器给出了:
ret ptr=0x00007fffc5d85690 inside 0x00005650a61cfaa0
ret ptr=0x00007fffc5d85690 inside 0x00005650a61cfaa0 value=9
ret ptr=0x00007fffc5d85690 inside 0x00005650a572a348 value=76
Run Code Online (Sandbox Code Playgroud)
最后一行的问题是值突然改变,这是某种错误吗?