对于List带有n元素的元素,在x64机器上需要更多存储(如果有的话):
List<int>
Run Code Online (Sandbox Code Playgroud)
-要么-
List<long>
Run Code Online (Sandbox Code Playgroud)
我想这个问题可以改为:
在x64上,int占用的空间是否少于long?
我在一个类中使用此代码来使webbrowser控件访问一个网站:
void myClass::visitWeb(const char *url)
{
WCHAR buffer[MAX_LEN];
ZeroMemory(buffer, sizeof(buffer));
MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, url, strlen(url), buffer, sizeof(buffer)-1);
VARIANT vURL;
vURL.vt = VT_BSTR;
vURL.bstrVal = SysAllocString(buffer);
// webbrowser navigate code...
VariantClear(&vURL);
}
Run Code Online (Sandbox Code Playgroud)
我从另一个void函数调用visitWeb,该函数在app的handlemessage()上调用.我需要在这里做一些内存释放吗?我看到varurClear正在释放vURL,但是我应该为缓冲区释放内存吗?我被告知在另一个bool我在同一个应用程序中我不应该释放任何东西,因为当bool返回true/false时一切都清除了,但是这个空白会发生什么?
假设我有以下C++类:
class Foo
{
double bar(double sth);
};
double Foo::bar(double sth)
{
double a,b,c,d,e,f
a = b = c = d = e = f = 0;
/* do stuff with a..f and sth */
}
Run Code Online (Sandbox Code Playgroud)
函数bar()将在循环中被调用数百万次.显然,每次调用时,都必须分配变量a..f.通过使变量成为Foo类的a..f成员并在函数的入口点初始化它们,我能获得任何性能吗?另一方面,a..f的值将通过this->取消引用,所以我想知道它是否实际上不是可能的性能下降.通过指针访问值是否有任何开销?谢谢!
Instruments'Leaks告诉我这个UIImage正在泄漏:
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[imagesPath stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@.png", [postsArrayID objectAtIndex:indexPath.row]]]];
// If image contains anything, set cellImage to image. If image is empty, try one more time or use noImage.png, set in IB
if (image != nil){
// If image != nil, set cellImage to that image
cell.cellImage.image = image;
}
image = nil;
[image release];
Run Code Online (Sandbox Code Playgroud)
(类单元格(自定义表格视图单元格)也在dealloc方法中释放cellImage).
我不知道为什么它会泄漏,但肯定是这样.图像以cellForRowAtIndexPath:-method 多次加载.前三个单元的图像不会泄漏(130px高,所有空间都可用).
泄漏没有给我任何其他信息UIImage allocated here in the code leaks.
你能帮我解决一下吗?谢谢 :)
我很抱歉问这么简单的问题,但这是一个我无法找到答案的具体问题.
我不是本地的Objective-c程序员,所以如果我使用任何C#术语我会道歉!
如果我在test.h中定义一个对象
@interface test : something {
NSString *_testString;
}
Run Code Online (Sandbox Code Playgroud)
然后在test.m中初始化它
-(id)init {
_testString = [[NSString alloc] initWithString:@"hello"];
}
Run Code Online (Sandbox Code Playgroud)
然后我明白我会在dealloc中释放它,因为每个init都应该有一个版本
-(void)dealloc {
[_testString release];
}
Run Code Online (Sandbox Code Playgroud)
但是,我需要澄清的是,如果在init中,我使用其中一种快捷方法创建对象,我还是会在dealloc中释放它吗?这不会破坏"一个发布一个init"规则吗?例如
-(id)init {
_testString = [NSString stringWithString:@"hello"];
}
Run Code Online (Sandbox Code Playgroud) 我使用一个对象来获取一些值并返回这个值.将返回的值仍在此对象中.下面是代码:
XMLErrorParser *xmlErrorParser = [XMLErrorParser alloc];
[xmlErrorParser parseData: data];
return xmlErrorParser.errors;
Run Code Online (Sandbox Code Playgroud)
那么如何释放xmlErrorParser对象并返回它的值呢?谢谢.
在iPhone上使用objective-c,这段代码出了什么问题?是在泄漏记忆吗?为什么?我该如何正确地做到这一点?
NSMutableString *result = [NSMutableString stringWithFormat:@"the value is %d", i];
Run Code Online (Sandbox Code Playgroud)
...然后在我的代码中...我可能需要将其更改为:
result = [NSMutableString stringWithFormat:@"the value is now %d", i];
Run Code Online (Sandbox Code Playgroud)
我需要第二次使用stringWithFormat ...但是不是创建一个新字符串并且没有正确释放旧字符串吗?
iphone memory-management objective-c stringwithformat nsmutablestring
如果在释放之前dealloc方法中对象的保留(引用)计数大于1,这是否意味着内存泄漏?
我正在调试我的代码以找到另一个问题,但后来遇到了这个微妙的问题.在dealloc方法中,我的一个对象的保留计数是3.此对象是具有retain的属性,仅在类中调用.现在我想在dealloc方法中所有对象的保留计数应该是1,然后才能正确释放?
这是自定义类中的dealloc方法示例:
- (void)dealloc {
// Prints: "myObject retaincount: 3"
NSLog(@"myObject retaincount: %d", [myObject retainCount]);
// myObject retain count will be 2 after this call
[myObject release];
[super dealloc];
}
Run Code Online (Sandbox Code Playgroud)
这是正常的吗?
我想问一下目标C中的内存管理问题.我是目标C的绿色.当我从Apple网站上阅读一些示例程序时,我看到了[XXX发布].我想这个语句用于释放变量的使用.但是,当我在程序中使用此语句时,我遇到了一些问题.我使用NSLog()来显示内容,但它无法显示内容,它显示了一些关于发布的声明.
目标C是否像java一样具有自动内存管理功能?或者我们需要关心程序的内存问题.
非常感谢你.
应该SomeClass*initialEl = new SomeClass [5]; 必须编译,假设SomeClass没有非公开声明的默认构造函数?考虑:
/*
* SomeClass.h
*
*/
#ifndef SOMECLASS_H_
#define SOMECLASS_H_
class SomeClass
{
public:
SomeClass(int){}
~SomeClass(){}
};
#endif /* SOMECLASS_H_ */
/*
* main.cpp
*
*/
#include "SomeClass.h"
int main()
{
SomeClass* initialEl = new SomeClass[5];
delete[] initialEl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) c++ memory-management class dynamic-memory-allocation dynamic-arrays
objective-c ×6
iphone ×5
c++ ×3
ios ×2
.net ×1
64-bit ×1
c# ×1
class ×1
dereference ×1
memory-leaks ×1
performance ×1
pointers ×1
retain ×1
uiimage ×1
visual-c++ ×1