好的,所以我有我的main.m程序代码,并且mvds建议我在创建新实例时释放我从类中借来的已分配内存。
出于某种原因,当我尝试使用
释放内存时[converter free];
它给了我一个警告,说转换器可能没有响应-free,一旦我完成我的程序,当我尝试循环回到开头,或者当我尝试终止程序时,它会吐出一堆垃圾
我认为这可能与我试图释放内存的地方有关,因为它可能已关闭,或者,它与我设置的循环有关。
这是我的main.m代码:
#import <Cocoa/Cocoa.h>
#import "class.h"
int main(int argc, char *argv[])
{
int result;
int prompt, prompt2;
int loop = 1;
double sourceTemp;
while (loop == 1) {
printf("\nWlecome to Bossverter, the one-stop shop for all of your temperature converting needs! Please choose a source temperature scale:\n[1] Fahrenheit\n[2] Celsius\n[3] Kelvin\n[4] Rankine\n\n");
result = scanf("%i", &prompt);
if (result != 1)
printf("Bossverter couldn't understand your input, Bossverter only needs one number!");
else if (result == EOF)
printf("Bossverter apologizes, Bossverter encountered an error when trying to read your input.");
else if (result == 1)
{
printf("\nNow, please enter the temperature you would like to convert:\n\n");
scanf("%lf", &sourceTemp);
TemperatureConverter *converter = [[TemperatureConverter alloc] init];
switch (prompt)
{
case 1:
//end-user chooses Fahrenheit
[converter setFahrenheitValue:sourceTemp];
break;
case 2:
//end-user chooses Celsius
[converter setCelsiusValue:sourceTemp];
break;
case 3:
//end-user chooses Kelvin
[converter setKelvinValue:sourceTemp];
break;
case 4:
//end-user chooses Rankine
[converter setRankineValue:sourceTemp];
break;
}
printf("\nNow, please choose a target temperature scale:\n[1] Fahrenheit\n[2] Celsius\n[3] Kelvin\n[4] Rankine\n\n");
scanf("%i", &prompt2);
char *scales[4] = { "Fahrenheit", "Celsius", "Kelvin", "Rankine" };
switch (prompt2)
{
case 1:
//end-user chooses Fahrenheit
printf("\n%lf degrees %s is %lf degrees Fahrenheit\n", sourceTemp, scales[prompt-1], [converter fahrenheitValue]);
break;
case 2:
//end-user chooses Celsius
printf("\n%lf degrees %s is %lf degrees Celsius\n", sourceTemp, scales[prompt-1], [converter celsiusValue]);
break;
case 3:
//end-user chooses Kelvin
printf("\n%lf degrees %s is %lf degrees Kelvin\n", sourceTemp, scales[prompt-1], [converter kelvinValue]);
break;
case 4:
//end-user chooses Rankine
printf("\n%lf degrees %s is %lf degrees Rankine\n", sourceTemp, scales[prompt-1], [converter rankineValue]);
break;
}
printf("\nI'm glad you just Bossverted something. Would you like to Bossvert again?\n[1] Yes!\n[2] No way man, I'm OUT!\n\n");
scanf("%i", &loop);
[converter free];
}
}
return 0;
}
答案 0 :(得分:2)
NSAutoreleasePool
- 这很危险(也可能是错误的)。release
,而不是free
。答案 1 :(得分:1)
free
是一个C库调用,而不是NSObject方法。您要查找的邮件是release
:
[converter release];