Post by Ulrich EckhardtPost by Luk Jack/// Test cMath
double ans = 1.0f;
cMath *m = new cMath();
ans = m->Power(3,2);
Formula fTest;
fTest.resize(2);
fTest[0].coefficient = 2;
fTest[0].degree = 3;
fTest[1].coefficient = 2;
fTest[1].degree = 1;
m->Derive(fTest);
fTest = m->getFormula();
if (m) {
delete m; // Leak (if absent) or Crash (if present)
m = NULL;
}
1. Wrapping something into a class that only contains functions and no
actual state (which is what your cMath class looks like, though I'm not
sure) is wrong. If you need, use a namespace instead. I guess you have this
habit and probably a few others from Java or similar languages that force
you to use classes for everything.
2. Don't dynamically allocate things unless you have a good reason to. Just
create a local instance of class cMath if you need.
3. If you really need dynamically allocated objects, use a "smart pointer"
to automatically delete them - C++ doesn't have a garbage collector.
std::auto_ptr would be one, the other would be std::tr1::shared_ptr or
boost::shared_ptr. Note that the former guarantees exclusive ownership
while the other models shared ownership.
4. You don't have to check a pointer before invoking delete on it.
Summary: The problem is in the code you don't show.
Uli
--
C++ FAQ:http://parashift.com/c++-faq-lite
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
Changed according to your suggestions. But this time I have heap
corruption. When I comment out this block of code,
everything runs normally. Any further advice please?