ok, thanks! i go try it out
ok, thanks! i go try it out
What is usi? If that is an object you really need a better way to distinguish it.Code:usi pos=0, item=0;
I apologise, usi is actually int
I've had a lot of troubles relying on the information spit out from the debugger and from the MFC overloaded new and delete.
At times I've used _CrtSetBreakAlloc(allocation_number) as Salem has suggested and found that the 'leak' is coming from an array that was allocated on the stack. It seems that at times the dump is performed prior to your objects actually being destroyed which means they will definitely be reported as leaks. However, your code may clean these up but b/c the dump was performed before this cleanup, you get incorrect information.
I've also experience no dump at all when in fact _crtDumpMemoryLeaks() returned true indicating a memory leak somewhere. Very odd.
The best way to perform debugging is to ignore that dump crapola and set crt debug hooks and let the crt debug library call your functions when something goes wrong. Then you can spit out information that is important to your code. Look in the manual on setting hooks and the _CrtMemState structure.
You can also use exception handling within try catch blocks to help you debug your code.
You can also create your own exception class and throw it.Code:try { int *pArray=new int[1000]; if (!pArray) throw std::bad_alloc; } catch (std::bad_alloc ba) { cout << ba.what() << endl; //Decide here if you can recover gracefully or just exit }
Code:class CMyException { char *m_pText; char *m_pFile; int m_iLine; public: CMyException(char *pText,char *pFile,int iLine):m_pText(pText),m_pFile(pFile),m_iLine(iLine) { } void ShowError(void) { //Show the error here } };Code:... try { CObject *pObject=new CObject(); if (!pObject) throw CMyException("Allocation failure for CObject",__FILE__,__LINE__); } catch (CMyException &me) { me.ShowError(); }
Last edited by VirtualAce; 10-10-2006 at 04:52 AM.
How to explain this:
My output is:Code:#include <iostream> #include <vector> using namespace std; class Test { int x; public: Test() { cout << "Constructor!" << endl; } ~Test() { cout << "Destructor!" << endl; } }; int main() { vector <Test> v; Test t1, t2, t3; v.push_back(t1); v.push_back(t2); v.push_back(t3); v.clear(); }
Constructor!
Constructor!
Constructor!
Destructor!
Destructor!
Destructor!
Destructor!
Destructor!
Destructor!
Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem
You're pointing out the discrepancy between the number of times the constructor and destructor have been called? You need to add a copy constructor to your code and then see what happens. Each push_back call is calling the copy constructor to create a copy of the objects you push onto the vector. This is where the 3 missing constructor calls are hiding.Originally Posted by Micko
Should generate output similar to:Code:#include <iostream> #include <vector> using namespace std; class Test { int x; public: Test() { cout << "Constructor!" << endl; } Test(const Test& test) { x = test.x; cout << "Copy constructor!" << endl; } ~Test() { cout << "Destructor!" << endl; } }; int main() { vector <Test> v; Test t1, t2, t3; v.push_back(t1); v.push_back(t2); v.push_back(t3); v.clear(); }
Constructor!
Constructor!
Constructor!
Copy constructor!
Copy constructor!
Copy constructor!
Destructor!
Destructor!
Destructor!
Destructor!
Destructor!
Destructor!
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
That explains things. Thank you!Originally Posted by hk_mp5kpdw
Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem