Archived

This topic is now archived and is closed to further replies.

kudi

operators new/delete in constructor/destructor

Recommended Posts

hi, in one of my classes i have a dynamic array: class example { private: char* data; public: example(); ~example(); } i want to initialize ''data'' in the constructor and after that delete ''data'' in the destructor. example::example() { data=new char[SIZE]; } example::example() { if(data) { delete [] data; data=NULL; } } i think that''s nothing very difficult and i hope that i haven''t made any mistake. i use this class every frame, i mean the constructor/destructor is called every frame. I use a simple DOS console. void main() { while(1) { example var; } } my problem is, everything works fine, if SIZE is small like 4, but if SIZE ist for example 4000, after some time, my program breaks down. do you have any ideas why ?

Share this post


Link to post
Share on other sites
the destructor wasn't the fault, actually i wrote it correct.

in meantime i don't think it has to do with the SIZE. perhaps i really have another mistake.

the only thing i can say, that there is an error on the screen, that has to do with the file dbgheap.c or another similar one (and something like pHeap!=......).
but i can't find the file on my disk.

[edited by - kudi on October 3, 2002 6:54:37 PM]

Share this post


Link to post
Share on other sites
Quick Tips:

1. The size should not matter.
2. It is perfectly safe to delete a null pointer.
3. You need not set your pointer to null in the destructor.

Check somewhere else in your engine for the problem, and post code if you need help.

Share this post


Link to post
Share on other sites
If the memory is for some odd reason not getting allocated in the constructor, which I would highly doubt, you might be trying to delete data that''s not yours if the pointer is not initialized to NULL. Just as a precaution, set the pointer to NULL before the memory allocation and check to see if it''s still NULL after the allocation, and if it is, print out an error message.

Share this post


Link to post
Share on other sites
Thanks for the answers, I first have to try out these things. This takes some time. Because it is a runtime error, i must run the program very often to be sure that there''s no error. (but you never can be really sure)

Share this post


Link to post
Share on other sites
quote:
If the memory is for some odd reason not getting allocated in the constructor, which I would highly doubt, you might be trying to delete data that''s not yours if the pointer is not initialized to NULL.

The C++ operator new never returns an invalid pointer. If it cannot allocate the requested amount of memory for some reason, it will throw an exception in which case 1) the pointer is not assigned a value and 2) the destructor is completely irrelevant because it never gets called.

(There is a variant of new that uses nothrow and returns a null pointer if the memory cannot be allocated, but the original poster is not using it. Even so, deleting a null pointer does nothing.)

quote:
Just as a precaution, set the pointer to NULL before the memory allocation and check to see if it''s still NULL after the allocation, and if it is, print out an error message.

Even if operator new did return a null pointer - which it does not - setting the pointer to null immediately before assigning it the value of new is pointless.

Share this post


Link to post
Share on other sites
stoffel, sorry, i dont understand what you mean, perhaps it is because i am not so good in english.

what do you mean with "copied by value", or how can i solve the problem?

Share this post


Link to post
Share on other sites
Class a; // This allocates memory on construction
Class b; // This also allocates memory on construction

a = b; // a and b now use the memory b was pointing to

// a and b get destructed, and free the same data (crash!)
// also the memory a used before is leaked.


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

Share this post


Link to post
Share on other sites
my application is multithreaded. could that be the problem?

and another question:
which runtime library do i have to select in the "project settings -> Code generation", or what are the differences. can anybody explaine that?

-multithreaded
-multithreaded DLL
-debug multithreaded
-debug multithreaded DLL


Share this post


Link to post
Share on other sites
Kudi:

"Pass-by-value" means that the object is being passed into a function by value instead of by pointer or reference (which are both referred-to as "pass-by-reference", which is admittedly confusing).

Example of how your program will crash with your object constructed as you say:

  
void foo (example e) // pass-by value

{
// doesn''t really need to do anything..

}

void main ()
{
while (true)
{
example e; // ctor called, data allocated at 0x12345678

foo (e); // creates a copy of data by value--now there

// is a second example object with data at

// 0x12345678

// as function returns, object is destroyed and

// 0x12345678 is deleted

} // end of loop; ~example is called on e, trying to delete

// 0x1234567, but it''s already been deleted. Crash.

}

Now, this will crash instantly. If it''s crashing after a while, then something else is going on. If possible, you might have to show us your code of the main loop in its entirety, and at least the function declarations of every function you call.

Share this post


Link to post
Share on other sites
show us the real code, please.

also why do you need to do 'new'?
char data[SIZE]  

works just as well as long as SIZE is a constant, known at compile time.

if you're using c++ and need a variable size try using vector<char> as that can be resized and won't cause you memory allocation issues.

[edited by - petewood on October 7, 2002 1:51:41 PM]

Share this post


Link to post
Share on other sites
thanks for all your answers, but i think i know what my mistake was (i am still not sur, because it is a runtime error and it sometimes takes 10 minutes until it appears). i didn''t chose multihreaded in /project settings/code generation/runtime library

actually i am a bit confused, because i think i had activated it once it and it still didnt function, but i will see. or it also could be the printf function which needs mutexes?

Share this post


Link to post
Share on other sites
Many standard library functions are not threadsafe. That''s what the multi-threaded runtime is for. Using the single-threaded runtime can cause crashes or undefined behaviour at seemingly random times.

Share this post


Link to post
Share on other sites