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. Memory deallocation problem
QtWS25 Last Chance

Memory deallocation problem

Scheduled Pinned Locked Moved C++ Gurus
9 Posts 6 Posters 4.6k 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.
  • I Offline
    I Offline
    Indrajeet
    wrote on last edited by
    #1

    Hi All

    I have created a dll which has a function whose return type is char*
    in this function i have allocated a memory for char* which will be
    returned to the calling function.My problem is where & how to deallocated this memory.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      thisisbhaskar
      wrote on last edited by
      #2

      I would suggest to provide a function inside dll to delete it and client calls it when it wants to delete the allocated memory. This is always good as only dll know how the memory is allocated.

      If it is allocated with malloc, it should be freed with free and if its allocated with new, then it should be freed with delete.

      If you don't like the above approach, you should document your dll function saying that caller should take care of memory deletion and how to delete it ( with free or delete )

      1 Reply Last reply
      0
      • K Offline
        K Offline
        koahnig
        wrote on last edited by
        #3

        An alternative would be to use smart or shared pointers "QSharedPointer":http://doc.qt.nokia.com/4.7/qsharedpointer.html .
        There is already some discussion on the ups and downs "Smart Pointers in Qt":http://developer.qt.nokia.com/forums/viewthread/5568/

        You need to decide which model suits your application best.

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        0
        • C Offline
          C Offline
          cincirin
          wrote on last edited by
          #4

          [quote author="Vijay Bhaska Reddy" date="1309755981"]I would suggest to provide a function inside dll to delete it and client calls it when it wants to delete the allocated memory. This is always good as only dll know how the memory is allocated. [/quote]
          If the function is declared as above (returning char*), this is the only safe solution.

          [quote author="Vijay Bhaska Reddy" date="1309755981"]If it is allocated with malloc, it should be freed with free and if its allocated with new, then it should be freed with delete.[/quote]
          This is true only if the client uses the same version of compiler (e.g. VC 2008, SP1)

          Also, here there is a possibility to let the client to allocate memory. You can declare function as: @int getString(char* string)@, and you return the number of string characters, and if the client passed NULL pointer, do nothing, just return the number of characters.

          1 Reply Last reply
          0
          • T Offline
            T Offline
            thisisbhaskar
            wrote on last edited by
            #5

            [quote author="cincirin" date="1309764013"]
            This is true only if the client uses the same version of compiler (e.g. VC 2008, SP1)

            Also, here there is a possibility to let the client to allocate memory. You can declare function as: @int getString(char* string)@, and you return the number of string characters, and if the client passed NULL pointer, do nothing, just return the number of characters.
            [/quote]

            Did not get these points, can you please elaborate.

            1. Is new/delete, malloc/free related to compiler?
            2. Where this function "getString" should be? Inside dll or in the client code?
            1 Reply Last reply
            0
            • C Offline
              C Offline
              cincirin
              wrote on last edited by
              #6

              If a block of memory is allocated with new or m(c)alloc then this block of memory should be released with delete or free with the same version of compiler that generated that block of memory with new/malloc. In other words if a block of memory is allocated with VC then its' not safe to release this memory with GCC or Watcom or whatever ... Also it's unsafe even if it uses the same compiler, but different version, e.g. new from VC2005 and delete from VC2008.

              Regarding getString, I suggested to change their definition in dll (not in client).
              @
              int getString(char* string)
              {
              int len = strlen(localDllString);
              if (NULL != string)
              strcpy(string, localDllString);
              return len;
              }
              @
              If client passed NULL string, this function only return the number of necessary characters. Client allocate, obtain and safe release the string with:
              @char* string = new char[getString(NULL)];
              getString(string);
              // to do with string ...
              delete []string;
              @

              1 Reply Last reply
              0
              • T Offline
                T Offline
                thisisbhaskar
                wrote on last edited by
                #7

                @cincirin, thanks for the information. I got it now and I like the second approach you have mentioned with getString() function :).

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  FlorentinaKosse9
                  wrote on last edited by
                  #8

                  @
                  template <class T>
                  class Matrix
                  {
                  private:
                  int dim1;int dim2;
                  T *mtx;
                  public:
                  Matrix(int a)
                  {
                  dim1=dim2=a;
                  mtx=new T
                  [dim1];
                  for(int i=0;i<dim1;i++)
                  mtx[i]=new T[dim2];
                  }
                  ~Matrix()
                  {
                  delete []mtx;
                  }
                  Matrix<T> operator +(Matrix<T> M)
                  {
                  Matrix C(dim1,dim2);
                  for(int i=0;i<dim1;i++)
                  for(int j=0;j<dim2;j++)
                  C.mtx[i][j]=mtx[i][j]+M.mtx[i][j];
                  return C;
                  }
                  };
                  @

                  How to deallocate the memory this above code?

                  [url=http://www.retirement-income.net/blog/minimumiradistribution]minimum ira distribution[/url]

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    lgeyer
                    wrote on last edited by
                    #9

                    @
                    for(int i=0;i<dim1;i++)
                    delete[] mtx[i];
                    delete[] mtx;
                    @

                    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