views:

27

answers:

2

OK, so I have my main.m program code, and mvds suggested I free the allocated memory I borrowed from my class when I created a new instance.

For some reason, when I attempt to free the memory using

[converter free];

It gives me a warning saying that converter may not respond to -free, and once I finish my program, it spits out a bunch of junk when I try to loop back to the beginning, or when I try to terminate the program.

I think it may have to do with either where I'm trying to free the memory, as it may be off, or, it has to do with my loop I have set up.

Here is my main.m code:

#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;
}
+2  A: 
  1. You don't have an NSAutoreleasePool - that's dangerous (and probably wrong).
  2. Use release, not free.
Carl Norum
+1  A: 

free is a C library call, not an NSObject method. The message you're looking for is release:

[converter release];
walkytalky