内存泄漏没有内存泄漏

时间:2018-02-18 06:29:00

标签: swift memory-management memory-leaks automatic-ref-counting

我在TabBarController的NavigationController中使用ViewController获得了Storyboard。 当用户点击“Logout”时,该VC呈现模态控制器,然后,当用户再次登录时,解除该模态窗口。 我清楚地看到模式viewController在执行dismiss时已被删除,但是如果我重复登录/注销sevaral次,我看到内存使用量增加了我重复登录/注销的次数。没有数据传递,没有代表等 Prifile - >泄漏显示没有泄漏,但内存仍然消失。 只有简单的三行代码,什么是错的?

#include <stdio.h>
#include <stdlib.h>

/* declare a struct to associate all the values */
typedef struct {
    int intr_class,
        freq,
        class1,
        cpi_1,
        class2,
        cpi_2,
        class3,
        cpi_3;
} parameters_t;

/* function to empty remaining characters in stdin before next input
 * to help avoid the pitfalls associated with using scanf for user input.
 */
void emptystdin()
{
    int c = getchar();
    for (; c != '\n' && c != EOF; c = getchar()) {}
}

/* params takes pointer to (address of) a struct parameters_t and
 * prompts for and fills each value. a tmp struct is used to avoid
 * changing any values in 'p' in case of a partial fill. returns
 * address of p on success, NULL otherwise indicating error.
 */
parameters_t *params (parameters_t *p){

    parameters_t tmp = { .intr_class = 0 };
    printf ("Enter the number of instruction classes: ");
    if (scanf ("%d", &tmp.intr_class) != 1)
        return NULL;

    printf ("Enter the frequency of the machine (MHz): ");
    if (scanf ("%d", &tmp.freq) != 1)
        return NULL;

    printf ("Enter CPI of class 1: ");
    if (scanf ("%d", &tmp.cpi_1) != 1)
        return NULL;

    printf ("Enter instruction count of class 1 (millions): ");
    if (scanf ("%d", &tmp.class1) != 1)
        return NULL;

    printf ("Enter CPI of class 2: ");
    if (scanf ("%d", &tmp.cpi_2) != 1)
        return NULL;

    printf ("Enter instruction count of class 2 (millions): ");
    if (scanf ("%d", &tmp.class2) != 1)
         return NULL;

    printf ("Enter CPI of class 3: ");
    if (scanf ("%d", &tmp.cpi_3) != 1)
        return NULL;

    printf ("Enter instruction count of class 3 (millions): ");
    if (scanf ("%d", &tmp.class3) != 1)
        return NULL;

    *p = tmp;   /* assign temp values to struct p */

    return p;
}

/* simple function to print values stored in p */
void prnparams (parameters_t *p)
{
    if (!p || p->intr_class == 0) {
        fprintf (stderr, "error: parameters empty or NULL\n");
        return;
    }
    printf ("parameters:\n"
            "  instruction classes: %d\n"
            "  frequency (MHz)    : %d\n"
            "  CPI of class 1     : %d\n"
            "  class 1 inst count : %d\n"
            "  CPI of class 2     : %d\n"
            "  class 2 inst count : %d\n"
            "  CPI of class 3     : %d\n"
            "  class 3 inst count : %d\n",
            p->intr_class, p->freq, p->cpi_1, p->class1,
            p->cpi_2, p->class2, p->cpi_3, p->class3);
}

int main (void) {

    char menuchoice;
    /* declare a struct & iniailize all values zero */
    parameters_t parameters = { .intr_class = 0 };  

    for (;;) {  /* loop until user quits */

        /* print out menu list */
        printf ("\nMenu of Options:\n"
                "______________\n"
                "  a) Enter Parameters\n"
                "  b) Calculate average CPI of a sequence of instructions\n"
                "  c) Calculate total execution time of a sequence of "
                      "instructions\n"
                "  d) Calculate MIPS of a sequence of instructions\n"
                "  p) Print stored values\n"
                "  e) Quit\n\n"
                "Enter selection: ");

        if (scanf ("%c", &menuchoice) == EOF) { /* check user cancels input */
            putchar ('\n');                     /* tidy up before exit */
            break;
        }
        if (menuchoice != '\n') /* make sure user didn't just hit [Enter] */
            emptystdin();       /* remove all chars from stdin */

        switch(menuchoice){
            case 'a':
                if (!params(&parameters))
                    fprintf (stderr, "error: params() failed.\n");
                emptystdin();  /* critical here or menuchoice would be '\n' */
                break;
            case 'b':
                //avgCPI();
                break;
            case 'c':
                //calcExTime();
                break;
            case 'd':
                //calcMIPS();
                break;
            case 'p':
                prnparams (&parameters);
                break;
            case 'e':
                exit(0);
            default:
                fprintf (stderr, "error: invalid menuchoice.\n");
                break;
        }
    }

    return 0;
}

模态

$ ./bin/scanfparams

Menu of Options:
______________
  a) Enter Parameters
  b) Calculate average CPI of a sequence of instructions
  c) Calculate total execution time of a sequence of instructions
  d) Calculate MIPS of a sequence of instructions
  p) Print stored values
  e) Quit

Enter selection: a
Enter the number of instruction classes: 10
Enter the frequency of the machine (MHz): 20
Enter CPI of class 1: 30
Enter instruction count of class 1 (millions): 40
Enter CPI of class 2: 50
Enter instruction count of class 2 (millions): 60
Enter CPI of class 3: 70
Enter instruction count of class 3 (millions): 80

Menu of Options:
______________
  a) Enter Parameters
  b) Calculate average CPI of a sequence of instructions
  c) Calculate total execution time of a sequence of instructions
  d) Calculate MIPS of a sequence of instructions
  p) Print stored values
  e) Quit

Enter selection: p
parameters:
  instruction classes: 10
  frequency (MHz)    : 20
  CPI of class 1     : 30
  class 1 inst count : 40
  CPI of class 2     : 50
  class 2 inst count : 60
  CPI of class 3     : 70
  class 3 inst count : 80

Menu of Options:
______________
  a) Enter Parameters
  b) Calculate average CPI of a sequence of instructions
  c) Calculate total execution time of a sequence of instructions
  d) Calculate MIPS of a sequence of instructions
  p) Print stored values
  e) Quit

Enter selection: e

这是几次登录/注销后的内存图

enter image description here

UPD

在Deinit之前

enter image description here

Deinit之后

enter image description here

UPD2:Zombies

enter image description here

0 个答案:

没有答案