delete [] question (SOLVED)

Started by
10 comments, last by Daaark 18 years, 10 months ago
I think one or both of us is/are misundertanding the other. Your original question was based around the problem that you were experiencing double-deletion errors and you asked how to check if you are double-deleting. There is no function hasThisMemoryBeenDeletedAlready, so you can't do:
c_model::~c_model(){    //delete the model data if it hasn't already happened    if (!hasThisMemoryBeenDeletedAlready(m_vtx)) delete [] m_vtx;    if (!hasThisMemoryBeenDeletedAlready(m_vtx)) delete [] m_nrm;    if (!hasThisMemoryBeenDeletedAlready(m_vtx)) delete [] m_uvw;    if (!hasThisMemoryBeenDeletedAlready(m_vtx)) delete [] m_tri;            return;}

Instead there are techniques to solve this problem, the most common of which I listed in my first reply. My reply was aimed at situations where multiple objects could own a single pointer, since this is the most common situation to your problem.

Next you stated that this was not the situation you were dealing with and that the data was entirely contained within your class. For this case since data should be allocated exactly once and deallocated exactly once it makes sense to put the allocation and deallocation code into functions that can only legally be called once, the constructor and destructor. You simply should not be suffering double deallocation problems with this.

At this point you seemed to favour a technique which I consider dangerous. While it may be sufficient in your case it is brittle in others. I posted an example of such a situation, showing that wrapped pointers are safer. I was going to post an example based on exception safety, which you may have issues with in your case, but did not want to overcomplicate the matter.

The point is that if you follow my suggestions and allocate in the constructor/deallocate in the destructor or wrap your pointers your code will work and will require minimal work to keep safe if you modify it. The setting pointers to null method on the other hand may cause you significant problems later on if you discover the need to make your model objects copyable.

I'm sorry if either of my posts seemed rude, I was simply finding it difficult to comprehend what your exact problem was. I think I now understand that you were doing i.e.:
class Model{	public:		Model()		{			array_ = new int[3];		}		~Model()		{			if (!array_)			{				delete[] array_;			}		}		void kill()		{			delete[] array_;		}	private:		int * array_;};int main(){	Model model;	model.kill();}


All you are doing here is adding unneccessary work with the kill function. Follow RAII (allocate in the constructor, deallocate in the destructor) and/or wrap your pointers. Trust me, it will lead to safer, more robust code.

Enigma
Advertisement
Quote:Original post by Enigma
All you are doing here is adding unneccessary work with the kill function.


My destructors only delete incase I forget while I'm doing a quick app to test something. My Kill functions serve a few purposes... in the case of the c_model, it destroys gl display lists, and resets other member variables back to the default state, so another model may be loaded into the same object.

Quote:but did not want to overcomplicate the matter.
[totally]
Quote:I'm sorry if either of my posts seemed rude
No, not at all.

You're just going out of your way to understand and solve a problem that doesn't exist? You're looking too deep into it. Nor am I going to install and learn to use boost to solve problems I don't have. [lol]

This topic is closed to new replies.

Advertisement