Memory deallocation problem
-
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. -
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 )
-
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.
-
[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.
-
[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.
- Is new/delete, malloc/free related to compiler?
- Where this function "getString" should be? Inside dll or in the client code?
-
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;
@ -
@cincirin, thanks for the information. I got it now and I like the second approach you have mentioned with getString() function :).
-
@
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?