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. How to delete char Array-Memory leak detection

How to delete char Array-Memory leak detection

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structuresdebuggingperformancetutorial
9 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.
  • A Offline
    A Offline
    Anu_Bala
    wrote on last edited by
    #1

    Hi, Im declaring one char array in dialog Header File.

    char *czTagGetVal;

    And in one dialog class,im using that array as

    void CGraphicsTagDlg:: GetTagLbl(int iLNo)
    {
    czTagGetVal = new char[50];
    memset(czTagGetVal,0,50);
    .
    .
    .
    }

    After debugging i get the solution without any error.But when i close the applcaition,in Output window i saw lot of Memory leak detection. So while closing the application i delete this array as

    if(czTagGetVal)
    delete [] czTagGetVal;

    But i get the error as

    Windows has triggered a breakpoint in GraphicsTag.exe.
    This may be due to a corruption of the heap, which indicates a bug in GraphicsTag.exe or any of the DLLs it has loaded.

    How can i avoid this error.I used such kind of array in my applcation by using new operator.I want to delete all allocated memory.How can i do that.Pls help me.

    Anu

    C C A 3 Replies Last reply
    0
    • A Anu_Bala

      Hi, Im declaring one char array in dialog Header File.

      char *czTagGetVal;

      And in one dialog class,im using that array as

      void CGraphicsTagDlg:: GetTagLbl(int iLNo)
      {
      czTagGetVal = new char[50];
      memset(czTagGetVal,0,50);
      .
      .
      .
      }

      After debugging i get the solution without any error.But when i close the applcaition,in Output window i saw lot of Memory leak detection. So while closing the application i delete this array as

      if(czTagGetVal)
      delete [] czTagGetVal;

      But i get the error as

      Windows has triggered a breakpoint in GraphicsTag.exe.
      This may be due to a corruption of the heap, which indicates a bug in GraphicsTag.exe or any of the DLLs it has loaded.

      How can i avoid this error.I used such kind of array in my applcation by using new operator.I want to delete all allocated memory.How can i do that.Pls help me.

      Anu

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      You should change:

      Anu_Bala wrote:

      char *czTagGetVal;

      to

      char *czTagGetVal = NULL;

      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.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      1 Reply Last reply
      0
      • A Anu_Bala

        Hi, Im declaring one char array in dialog Header File.

        char *czTagGetVal;

        And in one dialog class,im using that array as

        void CGraphicsTagDlg:: GetTagLbl(int iLNo)
        {
        czTagGetVal = new char[50];
        memset(czTagGetVal,0,50);
        .
        .
        .
        }

        After debugging i get the solution without any error.But when i close the applcaition,in Output window i saw lot of Memory leak detection. So while closing the application i delete this array as

        if(czTagGetVal)
        delete [] czTagGetVal;

        But i get the error as

        Windows has triggered a breakpoint in GraphicsTag.exe.
        This may be due to a corruption of the heap, which indicates a bug in GraphicsTag.exe or any of the DLLs it has loaded.

        How can i avoid this error.I used such kind of array in my applcation by using new operator.I want to delete all allocated memory.How can i do that.Pls help me.

        Anu

        C Offline
        C Offline
        Code o mat
        wrote on last edited by
        #3

        Without seeing more of your code i have 2 guesses: you are either writing into memory out of bounds (so e.g. you allocated space for 50 characters but are writing more than that into the buffer, 51 or 100 or... The other guess is, you are trying to free up memory you didn't allocate OR you are trying to free up the same block of memory twice.

        > The problem with computers is that they do what you tell them to do and not what you want them to do. < > //TODO: Implement signature here<

        A 1 Reply Last reply
        0
        • C Code o mat

          Without seeing more of your code i have 2 guesses: you are either writing into memory out of bounds (so e.g. you allocated space for 50 characters but are writing more than that into the buffer, 51 or 100 or... The other guess is, you are trying to free up memory you didn't allocate OR you are trying to free up the same block of memory twice.

          > The problem with computers is that they do what you tell them to do and not what you want them to do. < > //TODO: Implement signature here<

          A Offline
          A Offline
          Anu_Bala
          wrote on last edited by
          #4

          Im getting value by using this code

          glg_animation[iPage].viewport.GetResource("\\Tag\\String",&czTagGetVal);
          sTagName = (CString)czTagGetVal;

          In OnClose(),when i debug this part of code

          if(czTagGetVal)
          delete [] czTagGetVal

          czTagGetVal has value as "ALRMCAB ".So definitely it is less than 50 and also im allocating the memory using new and assign value also.But this error is coming. I delete this char array at the end of the fucntion where it is actually using.But the same error is coming.

          Anu

          C 1 Reply Last reply
          0
          • A Anu_Bala

            Im getting value by using this code

            glg_animation[iPage].viewport.GetResource("\\Tag\\String",&czTagGetVal);
            sTagName = (CString)czTagGetVal;

            In OnClose(),when i debug this part of code

            if(czTagGetVal)
            delete [] czTagGetVal

            czTagGetVal has value as "ALRMCAB ".So definitely it is less than 50 and also im allocating the memory using new and assign value also.But this error is coming. I delete this char array at the end of the fucntion where it is actually using.But the same error is coming.

            Anu

            C Offline
            C Offline
            Code o mat
            wrote on last edited by
            #5

            Shouldn't this:

            Anu_Bala wrote:

            glg_animation[iPage].viewport.GetResource("\\Tag\\String",&czTagGetVal);

            be this:

            glg_animation[iPage].viewport.GetResource("\\Tag\\String",czTagGetVal);

            ? But if you use this buffer in a function only, then why don't you simply declare it on the stack in the function?

            void This_is_the_function()
            {
            char czTagGetVal[50];
            ...
            }

            > The problem with computers is that they do what you tell them to do and not what you want them to do. < > //TODO: Implement signature here<

            A 1 Reply Last reply
            0
            • C Code o mat

              Shouldn't this:

              Anu_Bala wrote:

              glg_animation[iPage].viewport.GetResource("\\Tag\\String",&czTagGetVal);

              be this:

              glg_animation[iPage].viewport.GetResource("\\Tag\\String",czTagGetVal);

              ? But if you use this buffer in a function only, then why don't you simply declare it on the stack in the function?

              void This_is_the_function()
              {
              char czTagGetVal[50];
              ...
              }

              > The problem with computers is that they do what you tell them to do and not what you want them to do. < > //TODO: Implement signature here<

              A Offline
              A Offline
              Anu_Bala
              wrote on last edited by
              #6

              Actaully that GetResource() is third party fucntion(using DLL) and then GetResourceis decalred as

              GetResource( char * res_name, char ** s_value );

              So i have to use that.

              Anu

              C S 2 Replies Last reply
              0
              • A Anu_Bala

                Actaully that GetResource() is third party fucntion(using DLL) and then GetResourceis decalred as

                GetResource( char * res_name, char ** s_value );

                So i have to use that.

                Anu

                C Offline
                C Offline
                Code o mat
                wrote on last edited by
                #7

                That suggests that the function will allocate the string for you, so you don't need to do it, probably just free it (of course that depends on that 3rd party library). So like this:

                void this_is_the_function(...)
                {
                char *czTagGetVal = NULL;

                glg_animation[iPage].viewport.GetResource("\\Tag\\String",&czTagGetVal);
                sTagName = (CString)czTagGetVal;
                //Deallocate memory pointed by czTagGetVal????
                ...
                }

                Be careful with freeing up memory allocated by 3rd party things because if you don't do the de-allocation that is in pair with the allocation you can get unpredictable results.

                > The problem with computers is that they do what you tell them to do and not what you want them to do. < > //TODO: Implement signature here<

                1 Reply Last reply
                0
                • A Anu_Bala

                  Hi, Im declaring one char array in dialog Header File.

                  char *czTagGetVal;

                  And in one dialog class,im using that array as

                  void CGraphicsTagDlg:: GetTagLbl(int iLNo)
                  {
                  czTagGetVal = new char[50];
                  memset(czTagGetVal,0,50);
                  .
                  .
                  .
                  }

                  After debugging i get the solution without any error.But when i close the applcaition,in Output window i saw lot of Memory leak detection. So while closing the application i delete this array as

                  if(czTagGetVal)
                  delete [] czTagGetVal;

                  But i get the error as

                  Windows has triggered a breakpoint in GraphicsTag.exe.
                  This may be due to a corruption of the heap, which indicates a bug in GraphicsTag.exe or any of the DLLs it has loaded.

                  How can i avoid this error.I used such kind of array in my applcation by using new operator.I want to delete all allocated memory.How can i do that.Pls help me.

                  Anu

                  A Offline
                  A Offline
                  Albert Holguin
                  wrote on last edited by
                  #8

                  Possibilities for Error: - You've already deallocated this memory block. - If you allocated within a DLL, you should deallocate within the same DLL. Don't do memory management across DLL boundaries. Memory Leak: - If the function GetTagLbl() gets called more than once, you'll have leaking memory by only deleting once when closing.

                  1 Reply Last reply
                  0
                  • A Anu_Bala

                    Actaully that GetResource() is third party fucntion(using DLL) and then GetResourceis decalred as

                    GetResource( char * res_name, char ** s_value );

                    So i have to use that.

                    Anu

                    S Offline
                    S Offline
                    Stefan_Lang
                    wrote on last edited by
                    #9

                    Judging by that function signature, GetResource will either allocate a string for you, or return a pointer to a string it stores internally (or statically). In either case, the pointer you pass will be overwritten, and any memory previously allocated that this pointer used to point to will be lost! So, what you should do is: 1. do not allocate memory yourself! 2. initialize the pointer with NULL instead, before calling GetResource() 3. Check the documentation of GetResource() whether or not it allocates memory that needs to be freed or not. If your delete causes an error, then most likely it shouldn't be deleted, or it already gets deleted by whoever manages these resources.

                    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