|
Hi,
I declare CObject *obj;
I get some object in it from the array of objects of one perticular type of class.
and I modify that object.
Now I want to free obj. Should I directly make equate with NULL
or delete obj;
If I delete then the original object from the array will be lost.
How to prevent such kind of memory leak?
pathak
|
|
|
|
|
pathakr wrote: Now I want to free obj. Should I directly make equate with NULL
or delete obj;
Depends! If it's created with new then use delete . Else leave it alone. Of course you can set the object pointers to NULL afterwards.
Nibu thomas
A Developer
Programming tips[^] My site[^]
|
|
|
|
|
It was not created with new.
I declare it as CObject *obj=NULL;
then from a function I assign a object pointer to this one.
but then after modifying the object i wanted to vacate this one. (obj).
thanks
pathak
|
|
|
|
|
pathakr wrote: I declare it as CObject *obj=NULL;
then from a function I assign a object pointer to this one.
but then after modifying the object i wanted to vacate this one. (obj).
Then set it to NULL .
Nibu thomas
A Developer
Programming tips[^] My site[^]
|
|
|
|
|
|
pathakr wrote: Thanks Nibu.
You have to be careful here.
An example...
CAnotherObject anObject;
CObject *pToAnObject = &anObject;
pToAnObject = NULL;
Another case...
CAnotherObject *anObject = new CAnotherObject;
CObject *pToAnObject = anObject;
pToAnObject = NULL;
delete anObject;
Nibu thomas
A Developer
Programming tips[^] My site[^]
|
|
|
|
|
Thanks,
I understood .
pathak
|
|
|
|
|
|
Hi,
This may seem like a trivial question to all you Gurus, but how the heck do I fill a Roundrect with Gradient colors? I am using the Win32 SDK so MFC solutions are out of the question. I believe its possible by using regions but other than that, any solutions?
Thanks.
---
With best regards,
A Manchester United Fan
The Genius of a true fool is that he can mess up a foolproof plan!
|
|
|
|
|
GradientFill should help or use GDI+ . GDI+ provides the LinearGradientBrush and PathGradientBrush classes, both of which inherit from the Brush class.
Nibu thomas
A Developer
Programming tips[^] My site[^]
|
|
|
|
|
|
dear members,
Is there any way i can control the creation of the objects for a class, like for example we have a singleton class for which we can create only a single instance, likewise i wanna limit the creation of objects say only 2 objects for a class or only 3 object for the class
please give a small code example if possible.
thank u in advance
kumar_windows
|
|
|
|
|
The simple way
Create a static variable and in the contructor of the class keep on checking the value of it. If it exceeds the value (2 or 3 - exit)
ex:
class TestClass<br />
{<br />
static int i;<br />
public :<br />
TestClasss()<br />
{<br />
if(i>=3)<br />
exit(0);<br />
else<br />
i++;<br />
}<br />
};
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
kumar_windows wrote: Is there any way i can control the creation of the objects for a class, like for example we have a singleton class for which we can create only a single instance, likewise i wanna limit the creation of objects say only 2 objects for a class or only 3 object for the class
You can have a static variable to count the number of instances created. Increment variable in the constructor decrement in the destructor. Hence keeping track of the objects created.
The caveat is that you cannot entirely prevent the creation of an object but you can throw exceptions if object count increases beyond the limit that you have set.
Nibu thomas
A Developer
Programming tips[^] My site[^]
|
|
|
|
|
hi Friends,
my application is running different system. At that i need to Assign Cuurent system ip. any Functions or any Solutions pls help it me.
note: i used that place default ip 127.0.0.1 or 127.0.0.2
b.raju
|
|
|
|
|
Here is a function that does that...
Use the function as follows:
LPSTR lpszBuf;
GetIPAddress(lpszBuf);
<code></code>
<code>
delete [] lpszBuf;
BOOL GetIPAddress(LPSTR* lpszBuffer)
{
WSADATA wsaData;
::ZeroMemory(&wsaData, sizeof(WSADATA));
<code>
int retCode = WSAStartup(MAKEWORD(2,2), &wsaData);
if(retCode != 0)
return FALSE;
CHAR szHostName[MAX_COMPUTERNAME_LENGTH];
<code>
gethostname(szHostName, MAX_COMPUTERNAME_LENGTH);
LPHOSTENT hstEnt;
<code>
hstEnt = gethostbyname(szHostName);
if(hstEnt != NULL)
{
int nCount=0;
<code>
while(hstEnt->h_addr_list[nCount++]!=NULL)
;
<code>*lpszBuffer = new CHAR[20*nCount];</code>
**lpszBuffer = NULL;
int nIndex = 0;
<code>
while(hstEnt->h_addr_list[nIndex] != NULL)
{
strcat(
*lpszBuffer,(CHAR*)
inet_ntoa(*(struct in_addr *)hstEnt->h_addr_list[nIndex++])
);
if(nCount < nCount-1)
strcat(*lpszBuffer, (","));
}
}
else
return FALSE;
WSACleanup();
return TRUE;
}
The function that does the process of IP retrieval is the gethostbyname function. The above function just facilitates the use of this function.
Nibu thomas
A Developer
Programming tips[^] My site[^]
|
|
|
|
|
|
Hi,
I Have an Access DB in a MFC App, I add a record (db.AddNew, ..., db.Update)
later when I set strFilter and open database it returns EOF
I opened DB manually in MS Access, created a SQL query and, the same thing!
I then Copy - Pasted the record and rerun my App and it's OK
It is when My app writes a record, it can't be accessed by filter later, but if I just copy-paste record everythings goes right
The column in my filter is of type TEXT, and in my filter if I use 'like' instead of '=' the record will be found!
Can any one give me a discription of my crazy access DB?!!
//This is not a signature
while (I'm_alive) {
printf("I Love Programming");
}
|
|
|
|
|
Copy of earlier post however I also implemented the app in C++/CLI. If you've already read this then just skip to the bottom. Also of note: Using VS2005 & profiled the C++ code.
Did some work for a friend over the weekend. Basic scenario was that he had a client who wanted to have a program built that processed some numbers. He wanted a test app built in Java, C#, VB.NET and C++. My friend had already developed the C# app so he sent me the code and I translated to C++. Initial testing looked like the C# app was as fast and sometimes faster than the C++ app...in debug and release.
I found that true until I stopped writing to the console. And even more so when I used profile guided optimization. At that point the C++ app embarrased the C# app. And in the same way C# cuts in half the time for VB.NET. The client decided not to look at Java knowing it didn't stand much chance.
C++
Total Time: 25.47ms
Per Tick: 0.00029ms // note that I had to extend this just to see the result!
Per 50K Ticks: 14.62ms
C#
Total Time: 185.10ms
Per Tick: 0.0021ms
Per 50K Ticks: 106.23ms
C++/CLI
Total Time: 174.00ms
Per Tick: 0.0020ms
Per 50K Ticks: 99.86ms
Native C++ still!
ed
~"Watch your thoughts; they become your words. Watch your words they become your actions.
Watch your actions; they become your habits. Watch your habits; they become your character.
Watch your character; it becomes your destiny."
-Frank Outlaw.
-- modified at 23:09 Tuesday 23rd May, 2006
|
|
|
|
|
|
bob16972 wrote: Nish's numbers
It's a 4-year-ago article though. Maybe the numbers with VisualStudio 2005 may change.
Maxwell Chen
|
|
|
|
|
|
SaRath C wrote: http://msdn2.microsoft.com/en-us/library/bw65k95a.aspx[^]
Not getting your point... Would you be more specific please?!
Maxwell Chen
|
|
|
|
|
I'm not sure those numbers would tell anything about using C++ vs C# vs C++/CLI in a real world application using VS2005. What was done was to take a part of an algorithm that may or may not be used as part of a larger decision making application and benchmark using the various languages and VS2005. Optimizations that VS2005 provides played a big part in making the difference even wider. I don't think those were available when Nish did his tests.
You may want to take his tests and see where it goes with VS2005. I'm sold on C++ and VS2005 at this point!
ed
~"Watch your thoughts; they become your words. Watch your words they become your actions.
Watch your actions; they become your habits. Watch your habits; they become your character.
Watch your character; it becomes your destiny."
-Frank Outlaw.
|
|
|
|
|
Ed K wrote: I'm not sure those numbers would tell anything about using C++ vs C# vs C++/CLI in a real world
Ed K wrote: I'm sold on C++
I'm in total agreement with you. It seemed like you were in a benchmarking mood so I was just putting a road sign up to his article. I'm guessing most people are not as critical of the data they are fed and are willing to accept the results and the choice of the test parameters chosen by the authors. I prefer to be critical of everything I'm fed anymore but I lack the time and the talent to challenge his numbers as he seems to be the most knowledgeable person here concerning the CLI. I believe a little healthy debate or the possibility of a challenge helps keeps everything honest in the end and everyone benefits.
Take care.
|
|
|
|