|
-
August 1st, 2002, 08:29 AM
#1
Found my problem
I finally found the problem in my code.
I posted earlier that when I debug into my program it keeps crashing on a "new" operator but it runs fine. Now I found the cause of the problem. It is because I tried to access an object that was already deleted. It took me couple of days to find this problem. I don't know how you guys do it but I used _CrtCheckMemory to check the heap in all suspected areas. Right after I tried to access the deleted object, the program crashed in _CrtCheckMemory().
I still do not know why it runs fine and crashes when I debug it. Is the heap somehow different in the two cases?
-
August 1st, 2002, 09:14 AM
#2
That's one of the reasons why we keep going on about avoiding the use of naked pointers in your code. Wrap new and delete in the RAII idiom (resource acquisition is initialisation) - acquire a resource inthe class constructor and release it in the destructor. Use the STL containers, rather than newing memory for an array. Etcetera.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
August 1st, 2002, 12:40 PM
#3
Is this a C++ exception? I was not able to catch anything.
Here is the problem in my code:
class SomeObj
{
int x;
}
class A
{
SomeObj *obj;
}
main
{
A a;
SomeObj *newobj = new SomeObj;
a.obj = newobj;
// somewhere
delete newobj;
// Error! try to acess delete object
(a.obj)->x = 1;
}
-
August 2nd, 2002, 04:57 AM
#4
It's not an exception: it's undefined behaviour. IOW you can't predict what will happen if you do it (most likely is that your program will crash spectacularly or that you will trample over memory, and THEN your program will crash spectacularly).
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
August 2nd, 2002, 06:00 AM
#5
Thats also the reason to initialize all class objects to some known value, normally 0 or NULL in the class constructor and to reinitialize it after deleting a pointer. Makes it a lot easier to see the problem when you see the address of a pointer is 0.
-
August 18th, 2002, 11:23 AM
#6
This is how I would go about it:
class SomeObj
{
public:
SomeObj() : m_x(0) {}
SomeObj(const int x) : m_x(m) {}
protected:
int m_x;
}
class A
{
public:
A() : m_obj(new SomeObj) {}
protected:
std::auto_ptr<SomeObj> m_obj;
}
main
{
A a;
}
Regards, willchop
-
August 18th, 2002, 02:47 PM
#7
If you're going to use auto_ptr in your class, then you are going to have to write your own copy ctor and copy assignment operator (or, more likely, disable them both). The compiler generated function will almost certainly do exactly the wrong thing.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
August 19th, 2002, 12:37 PM
#8
A "deep copy" is also needed for classes with raw pointer member
variables. One solution would be to use the boost::shared_ptr.
Regards, willchop
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|