How to delete char Array-Memory leak detection
-
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
-
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
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] -
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
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<
-
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<
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 [] czTagGetValczTagGetVal 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
-
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 [] czTagGetValczTagGetVal 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
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<
-
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<
-
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
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<
-
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
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. -
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
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 callingGetResource()
3. Check the documentation ofGetResource()
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.