|
Yes. For instance, see [^].
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
|
|
|
|
|
What do you mean exactly by "valid" ? A simple rule that will work in most of the cases is to check if the pointer is NULL or not. If it is not NULL, then it SHOULD mean that the pointer is valid.
|
|
|
|
|
Well, it maybe not NULL but still not valid for the purpose, for instance pointing to garbage or to an instance of a different class.
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
|
|
|
|
|
That's why I said "in most of the cases"
If you have to check if your pointer is pointing to garbage (because for example you already destroyed the object), then I think it is a hint that your design is maybe not the best...
|
|
|
|
|
Cedric Moonen wrote: f you have to check if your pointer is pointing to garbage (because for example you already destroyed the object), then I think it is a hint that your design is maybe not the best...
That's probably matter of development rather than design. Anyway suppose the design is mine: the pointer maybe passed by someone else's 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.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
Cedric Moonen wrote: it is a hint that your design is maybe not the best...
I thought the same, but just in case of any rare error, I thought this check may help. But is it good design to surround all the functions by try{} catch(){} ?
Thanks,
Suman
|
|
|
|
|
rp_suman wrote: But is it good design to surround all the functions by try{} catch(){}?
Even this (the below code)? Handling exceptions when only needed.
Value MyClass::GetValue() {
return m_Value;
}
Maxwell Chen
|
|
|
|
|
Maxwell Chen wrote: Handling exceptions when only needed.
Yes, I think thats nice..
Thanks & Regards,
Suman
|
|
|
|
|
rp_suman wrote: Is it possible to check the pointer is valid pointer of that class by using RTTI or some other way?
Have you looked into the typeid operator?
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
DavidCrow wrote: Have you looked into the typeid operator?
I will look in that..
Thanks for the help.
Best Regards,
Suman
|
|
|
|
|
I am creating a pointer to some user defined class in OnNewDocument() function of my application's class derived from CDocument.
When I create new document and closing it, there is a memory leak report for the pointer to user defined class.
I have tried overriding OnCloseDocument() and deleting the pointer. But still the leak is there.
How to fix this leak?
Thanks,
Suman
|
|
|
|
|
Set the pointer to NULL in your constructor and whenever you need to create it again, check if the pointer is NULL. If that's not the case, then first delete the instance before creating a new one.
|
|
|
|
|
If you are using SDI, override the CDocument::DeleteContents() and do the cleanup as I remember correctly if you are calling file new command OnCloseDocument will not be called, instead OnNewDocument. And try debugging whether the object is deleted at OnCloseDocument. As other users replied to initialize NULL, you have to intialize to NULL in constructor rather than OnNewDocument() as there are cases OnNewDocument called twice. When ever you delete then set to NULL;
|
|
|
|
|
rp_suman wrote: When I create new document and closing it, there is a memory leak report...
In MFC, you don't see the result of a memory leak until the application is shut down. How are you verifying this?
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
DavidCrow wrote: In MFC, you don't see the result of a memory leak until the application is shut down. How are you verifying this?
Hi, You are right,
In debug mode, I create new document by File -> New, closing it and exit the application, there is a memory leak report in output window as:
normal block at 0x00F8A428, 416 bytes long. and the code at reported line is:
m_TList = new TList;
where m_TList is a pointer of class TList.
I use VLD usually to find memory leak. But most of memory leaks reported by VC itself.
Thanks & Regards,
Suman
modified on Monday, March 17, 2008 9:08 PM
|
|
|
|
|
So when m_TList is no longer needed, delete it.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Hi all..
How do i retrieve selected year,date,day, minute or second from datetime control??
Thanks
Ash..
|
|
|
|
|
It's Non-MFC Application.
Ash..
|
|
|
|
|
Why you are not using MSDN, see DateTime_GetSystemtime()
|
|
|
|
|
Have you considered the DTM_GETSYSTEMTIME message?
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
hi guys,
this program creates number spiral Matrix
For example :
Enter a number : 4
01 02 03 04
12 13 14 05
11 16 15 06
10 09 08 07
Code :
#include <iostream.h>
int main()
{
int UserNo=1; // default Value
int StoreArray[100][100];
cout<<endl<<"Please enter a number :";
cin>>UserNo;
int Sqr=UserNo*UserNo;
int r=1,c=UserNo;
StoreArray[UserNo][UserNo];
int n=1;
do
{
for (int b=r;b<=c;b++)
{
StoreArray[r][b]=n;
n++;
}
for (b=r+1;b<=c;b++)
{
StoreArray[b][c]=n;
n++;
}
n--;
for (b=c;b>=r;b--)
{
StoreArray[c][b]=n;
n++;
}
for (b=c-1;b>=r+1;b--)
{
StoreArray[b][r]=n;
n++;
}
r++;
c--;
}while (n<sqr);>
if (UserNo%2!=0)
{
int mid=((UserNo/2) +1);
StoreArray[mid][mid]=Sqr;
}
// DISPLAYING
for (int a=1;a<=UserNo;a++)
{
for (int b=1;b<=UserNo;b++)
{
cout<<StoreArray[a][b]<<" | ";
}
cout<<endl;
}
return 1;
}
|
|
|
|
|
AnkurS wrote: this program creates number spiral Matrix
Thanks for your effort!
Maxwell Chen
|
|
|
|
|
And... ? What is the question ?
|
|
|
|
|
Cedric Moonen wrote: And... ? What is the question ?
OP don't want to ask any questions, perhaps instead of posting an article he posted a message.
"Life was much simpler in 2002"
|
|
|
|
|
hi,
The following one shows hello world.
#include <stdio.h>
void main()
{
printf("hello world\n");
}
</stdio.h>
Greetings.
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
|
|
|
|