Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. memory leak in the code?

memory leak in the code?

Scheduled Pinned Locked Moved C / C++ / MFC
performancequestion
51 Posts 5 Posters 0 Views 1 Watching
  • 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.
  • CPalliniC CPallini

    You have always to do your cleanup stuff! :)

    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
    [my articles]

    E Offline
    E Offline
    Eytukan
    wrote on last edited by
    #9

    But I suggest he should do something like this : if(pMyStuff!=NULL) { delete pMyStuff; } :cool:


    OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus Best wishes to Rexx[^]

    CPalliniC 1 Reply Last reply
    0
    • E Eytukan

      huh Hamid! :| it's simply because he should have put it like:

      int* a;
      int* b;

      try { a= new int[N]; b= new int[M]; } catch (bad_alloc&) { cout <<"Error allocating memory!"; } wake up! :)


      OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus Best wishes to Rexx[^]

      H Offline
      H Offline
      Hamid Taebi
      wrote on last edited by
      #10

      Yeah it was a quick sample. ;)

      1 Reply Last reply
      0
      • E Eytukan

        huh Hamid! :| it's simply because he should have put it like:

        int* a;
        int* b;

        try { a= new int[N]; b= new int[M]; } catch (bad_alloc&) { cout <<"Error allocating memory!"; } wake up! :)


        OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus Best wishes to Rexx[^]

        P Offline
        P Offline
        pierre_ribery
        wrote on last edited by
        #11

        Not like that, see my earlier post. Always initialize pointers!! In this case set them to NULL(0). int* a = NULL; int* b = NULL;

        E 1 Reply Last reply
        0
        • P pierre_ribery

          Not like that, see my earlier post. Always initialize pointers!! In this case set them to NULL(0). int* a = NULL; int* b = NULL;

          E Offline
          E Offline
          Eytukan
          wrote on last edited by
          #12

          Who said I didn't do it? I'm a c++ programmer. class myclass { int* a; int* b; myclass() { a= NULL; b= NULL; } } ;P


          OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus Best wishes to Rexx[^]

          P 1 Reply Last reply
          0
          • E Eytukan

            But I suggest he should do something like this : if(pMyStuff!=NULL) { delete pMyStuff; } :cool:


            OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus Best wishes to Rexx[^]

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #13

            And you are right! :-D

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            [my articles]

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • E Eytukan

              Who said I didn't do it? I'm a c++ programmer. class myclass { int* a; int* b; myclass() { a= NULL; b= NULL; } } ;P


              OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus Best wishes to Rexx[^]

              P Offline
              P Offline
              pierre_ribery
              wrote on last edited by
              #14

              Your code said it. Anyway, I think it is pretty important to show it in the code as well.

              1 Reply Last reply
              0
              • P pierre_ribery

                In a case like this you should initialize both a and b to 0 before the try clause. You cannot delete a or b at this stage, I assume you will need to use them later, or what was the purpose of allocating them? int* a = NULL; int* b = NULL; try { a = new int [N]; b = new int [M]; } catch (bad_alloc) { // Tell the user if a or b failed... } // Do some stuff on a or b // Now delete if they are allocated if(a) delete a[]; if (b) delete b[]; Thanks!

                G Offline
                G Offline
                George_George
                wrote on last edited by
                #15

                Thanks pierre_ribery, I want to confirm with you that your point is we need to delete a or b if they are successful allocated, even if bad_alloc happens (may be caused by other statements), right? regards, George

                P 1 Reply Last reply
                0
                • H Hamid Taebi

                  Why you didnt use like this code try { int * a= new int[N]; int * b= new int[M]; } catch (bad_alloc&) { cout <<"Error allocating memory!"; }

                  G Offline
                  G Offline
                  George_George
                  wrote on last edited by
                  #16

                  Hi Hamid, I am confused. My question is about whether we need to delete a or b if bad_alloc happens, does your reply has anything related to my question? :-) regards, George

                  H 1 Reply Last reply
                  0
                  • CPalliniC CPallini

                    Yes. :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    [my articles]

                    G Offline
                    G Offline
                    George_George
                    wrote on last edited by
                    #17

                    Thanks for your confirmation, CPallini! regards, George

                    1 Reply Last reply
                    0
                    • G George_George

                      Hi Hamid, I am confused. My question is about whether we need to delete a or b if bad_alloc happens, does your reply has anything related to my question? :-) regards, George

                      H Offline
                      H Offline
                      Hamid Taebi
                      wrote on last edited by
                      #18

                      My reply was for check does it with success or no (and my suggestion is when you want to allocate or convert use of try/catch block) and when you got error means that it doesnt allocate any thing to variable.

                      G 1 Reply Last reply
                      0
                      • G George_George

                        Thanks pierre_ribery, I want to confirm with you that your point is we need to delete a or b if they are successful allocated, even if bad_alloc happens (may be caused by other statements), right? regards, George

                        P Offline
                        P Offline
                        pierre_ribery
                        wrote on last edited by
                        #19

                        Yes that was exactly my point! If you have allocated memory, then you have to delete it. Therefore it is vital to initialize your pointers to 0 before using them. Cheers, Pierre

                        G 1 Reply Last reply
                        0
                        • H Hamid Taebi

                          My reply was for check does it with success or no (and my suggestion is when you want to allocate or convert use of try/catch block) and when you got error means that it doesnt allocate any thing to variable.

                          G Offline
                          G Offline
                          George_George
                          wrote on last edited by
                          #20

                          Thanks Hamid, I have developed a couple of samples, which specific case do you think I need to check? regards, George

                          H 1 Reply Last reply
                          0
                          • P pierre_ribery

                            Yes that was exactly my point! If you have allocated memory, then you have to delete it. Therefore it is vital to initialize your pointers to 0 before using them. Cheers, Pierre

                            G Offline
                            G Offline
                            George_George
                            wrote on last edited by
                            #21

                            Thanks for your advice, Pierre! regards, George

                            1 Reply Last reply
                            0
                            • G George_George

                              Thanks Hamid, I have developed a couple of samples, which specific case do you think I need to check? regards, George

                              H Offline
                              H Offline
                              Hamid Taebi
                              wrote on last edited by
                              #22

                              If you check each block of your program(for exmaple is hwnd valid,etc) you can almost(not always) sure that you didnt get an exception when you run your program

                              G 1 Reply Last reply
                              0
                              • H Hamid Taebi

                                If you check each block of your program(for exmaple is hwnd valid,etc) you can almost(not always) sure that you didnt get an exception when you run your program

                                G Offline
                                G Offline
                                George_George
                                wrote on last edited by
                                #23

                                Hi Hamid, How could I check manually which block is exception safe or not? There are too many runtime errors, like out of memory or input invalid values to new which will cause bad_alloc. :-) regards, George

                                H 1 Reply Last reply
                                0
                                • G George_George

                                  Hi Hamid, How could I check manually which block is exception safe or not? There are too many runtime errors, like out of memory or input invalid values to new which will cause bad_alloc. :-) regards, George

                                  H Offline
                                  H Offline
                                  Hamid Taebi
                                  wrote on last edited by
                                  #24

                                  Well its simple you know some actions will be problem and you can anticipate them a short list like: (1) When you want to read a file or write a file:1-does file exist 2-does this file open with other programs 3- can you write to a file on the cd or no,does file on the floppy drive and does it write-protected or no (2) Database do you have access to database (3) when you need to a handle to a window does return value valid or its null (4) Picture does file a image file or no what was return value (5) when you want to read of internet do you have any connection to internet (6) do you have a valid pointer or its null (7) Dynamic memory,does it valid (8).... ------------------------------ After all of them you must free memory. ;)

                                  G 1 Reply Last reply
                                  0
                                  • H Hamid Taebi

                                    Well its simple you know some actions will be problem and you can anticipate them a short list like: (1) When you want to read a file or write a file:1-does file exist 2-does this file open with other programs 3- can you write to a file on the cd or no,does file on the floppy drive and does it write-protected or no (2) Database do you have access to database (3) when you need to a handle to a window does return value valid or its null (4) Picture does file a image file or no what was return value (5) when you want to read of internet do you have any connection to internet (6) do you have a valid pointer or its null (7) Dynamic memory,does it valid (8).... ------------------------------ After all of them you must free memory. ;)

                                    G Offline
                                    G Offline
                                    George_George
                                    wrote on last edited by
                                    #25

                                    Thanks Hamid, Comprehensive samples. regards, George

                                    H 1 Reply Last reply
                                    0
                                    • G George_George

                                      Thanks Hamid, Comprehensive samples. regards, George

                                      H Offline
                                      H Offline
                                      Hamid Taebi
                                      wrote on last edited by
                                      #26

                                      I glad I could help to you. ;)

                                      G 1 Reply Last reply
                                      0
                                      • H Hamid Taebi

                                        I glad I could help to you. ;)

                                        G Offline
                                        G Offline
                                        George_George
                                        wrote on last edited by
                                        #27

                                        Thanks Hamid, Hope we can have further discussion here -- other topics -- I will find one soon. :-) regards, George

                                        H 2 Replies Last reply
                                        0
                                        • G George_George

                                          Thanks Hamid, Hope we can have further discussion here -- other topics -- I will find one soon. :-) regards, George

                                          H Offline
                                          H Offline
                                          Hamid Taebi
                                          wrote on last edited by
                                          #28

                                          Very good. ;P

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups