Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Application Is Crashed When delete int*
Forum Updated to NodeBB v4.3 + New Features

Application Is Crashed When delete int*

Scheduled Pinned Locked Moved Unsolved C++ Gurus
10 Posts 5 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Ketan__Patel__0011K Offline
    Ketan__Patel__0011K Offline
    Ketan__Patel__0011
    wrote on last edited by
    #1

    Hello Friends And Qt Experts

    when i try to delete int* that time my application crashed.
    follow my example code.

    int* CorrectError()
    {
          int *Temp = new int[100];
          return Temp ;
    }
    
    int main()
    {
          int *data = CorrectError();
    
          ////// Some Code //////
          
          delete data ; ///// here my application is crashed.
          
          printf("Application Is Closed");
    }
    

    please help me to solve my problem
    thanks is advance.

    1 Reply Last reply
    0
    • GerhardG Offline
      GerhardG Offline
      Gerhard
      wrote on last edited by
      #2

      delete [] data ; ///// here my application is crashed.

      Gerhard

      Ketan__Patel__0011K 1 Reply Last reply
      1
      • GerhardG Gerhard

        delete [] data ; ///// here my application is crashed.

        Ketan__Patel__0011K Offline
        Ketan__Patel__0011K Offline
        Ketan__Patel__0011
        wrote on last edited by
        #3

        @Gerhard

        Thanks for Reply

        i am already try it

        but application is still crashing.

        JonBJ 1 Reply Last reply
        0
        • Ketan__Patel__0011K Ketan__Patel__0011

          @Gerhard

          Thanks for Reply

          i am already try it

          but application is still crashing.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Ketan__Patel__0011
          According to me: you do not want to delete data, as data was never newed. It was *data that was newed.

          ~~delete [] *data;~~
          

          EDIT
          Ignore above, nonsense, I should have tested.

          I find no problem here at all with your original delete data. Like @Christian-Ehrlicher has said below, all ought to be well. Use valgrind, and/or check what the stack trace from the "crash" you report says.

          Ketan__Patel__0011K 1 Reply Last reply
          0
          • JonBJ JonB

            @Ketan__Patel__0011
            According to me: you do not want to delete data, as data was never newed. It was *data that was newed.

            ~~delete [] *data;~~
            

            EDIT
            Ignore above, nonsense, I should have tested.

            I find no problem here at all with your original delete data. Like @Christian-Ehrlicher has said below, all ought to be well. Use valgrind, and/or check what the stack trace from the "crash" you report says.

            Ketan__Patel__0011K Offline
            Ketan__Patel__0011K Offline
            Ketan__Patel__0011
            wrote on last edited by
            #5

            @JonB said in Application Is Crashed When delete int*:

            delete [] *data;

            if i try delete[] *data that time i got this error.

            error: C2541: 'delete': cannot delete objects that are not pointers

            1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You program should not crash - all is fine (except the delete[] - thing but this does not lead to a crash in Linux or Windows nowadays).
              Use e.g. valgrind o see what really happens.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              1
              • Ketan__Patel__0011K Offline
                Ketan__Patel__0011K Offline
                Ketan__Patel__0011
                wrote on last edited by
                #7

                if i am ignore delete data;

                that i am got memory leakage in my application

                that's why i am try to delete the data.

                Christian EhrlicherC JonBJ KroMignonK 3 Replies Last reply
                0
                • Ketan__Patel__0011K Ketan__Patel__0011

                  if i am ignore delete data;

                  that i am got memory leakage in my application

                  that's why i am try to delete the data.

                  Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Ketan__Patel__0011 As I said you code is fine and it does not crash nor leak memory here when using delete[]

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  0
                  • Ketan__Patel__0011K Ketan__Patel__0011

                    if i am ignore delete data;

                    that i am got memory leakage in my application

                    that's why i am try to delete the data.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @Ketan__Patel__0011
                    I am wondering: is it the case that your progem does not crash on the line delete data? Rather, after you have done that, you still access data somewhere, but it has been deleted?? You do indeed need to delete data somewhere to prevent memory leak, but not while you are still using it!

                    Whenever you have a "crash", run your code in the debugger and look at the stack trace to see where the "crash" actually emanates from in your code....

                    1 Reply Last reply
                    0
                    • Ketan__Patel__0011K Ketan__Patel__0011

                      if i am ignore delete data;

                      that i am got memory leakage in my application

                      that's why i am try to delete the data.

                      KroMignonK Offline
                      KroMignonK Offline
                      KroMignon
                      wrote on last edited by KroMignon
                      #10

                      @Ketan__Patel__0011 said in Application Is Crashed When delete int*:

                      if i am ignore delete data;

                      If you are allocation with new [] you have to delete it with delete [], this is C++ standard.
                      If it doesn't work, then throw away your C++ compiler and use one which is standard conform.

                      This must work:

                      int *temp = new int[100];
                      ...
                      delete[] temp;
                      

                      => cf. http://www.cplusplus.com/reference/new/operator delete[]/

                      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved