Skip to main content
GameDev.net gamedev.net
🔒 Locked

delete on structs!?

Started by clabinsky Dec 22, 2001 at 11:10 PM 15 replies 550+ views
Original Post
clabinsky
clabinsky
Hi all. I have had a number of blue screens trying to collect garbage from two linked lists with structs. The structs are: struct BMP { BITMAPFILEHEADER m_BMPHead; BITMAPINFOHEADER m_BMPInfo; RGBQUAD m_Palette[BMP_COLORS]; BYTE *m_Image; BMP *m_lpNext; BMP *m_lpPrev; }; and struct SurfList { LPDIRECTDRAWSURFACE7 m_lpSurface; SurfList *m_lpNext; SurfList *m_lpPrev; }; 1. These are in linked lists. To clear the lists I loop through them keeping a ptr on the current and next. Would: delete lpBMP; and delete lpSurflist delete each of these structs, including the *m_image array? 2. How about the RGBQUAD does it get deleted? delete[] &m_Palette cause blue screen. 3. When releasing a primary surface, does the palette get cleared out as well (8 bit)? I currently delete the BMP in a loop like so (lpCurrent=*struct): delete[] lpCurrent->m_Image; lpCurrent->m_lpNext=0; lpCurrent->m_lpPrev=0; delete lpCurrent; Lets here the experts. Edited by - clabinsky on December 22, 2001 12:18:28 AM
Why make it simple when you can make it sooo nice and complicated?
Shannon Barber
Shannon Barber
How you need to delete it entirely depends on how it was allocated
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Colin Jeanne
Colin Jeanne
If you''re that paranoid about whether or not everything is deleted and cleaned up (which is good that you are), why not just make a deconstructor for the structs?

Invader X
Invader''s Realm
clabinsky
clabinsky
1. It was allocated loke so:
BMP *lpBMP;
lpBMP=new BMP;

SurfList *lpSurfList;
lpSurfList=new SurfList;

2.Deconstructor, ah yes, well, thing is that I will want to clear some of the classes and re-fill them at run time. To do that each class has a member function Clear(); that will do just that. You might already guess that my destructor calls Clear();

Oh, yes and if something goes wrong and I want to shut down I would like to decide the order in which the classes gets cleared (if a class member is reference by an outsider for example) which is why I created a common Clear() function. Make sence?

Edited by - clabinsky on December 23, 2001 1:09:19 AM
Why make it simple when you can make it sooo nice and complicated?
Colin Jeanne
Colin Jeanne
So your BMP and SurfList structs have Clear() which is also called by their deconstructors? Sounds good to me.

Invader X
Invader''s Realm
Qoy
Qoy
it''s *DESTRUCTOR* dammit!
clabinsky
clabinsky
quote:
Original post by Invader X
So your BMP and SurfList structs have Clear() which is also called by their deconstructors? Sounds good to me.



Hmm, no. Well, The structs BMP and SurfList are each included in a separate class. Both classes call their memberfunction Clear() in their respective destructors. The clear functions behave differently of course but the result is the same - it clears all outside references and resets the class to be used again or destroyed buy its destructor. The classes alse keep track of the "clear" status of the class to avoid repeated calls to Clear();

Do you still think this is OK or have I totally missed something. I can´t see any use of Clear() in the struct itself unless I´d call it with something like Clear(&StructBMP) to destroy itself. Since the class contains linked list of structs, Clear() deletes the whole list of structs and additional cleaning.

Am I OK here?


Edited by - clabinsky on December 23, 2001 3:41:32 AM
Why make it simple when you can make it sooo nice and complicated?
Colin Jeanne
Colin Jeanne
quote:
Original post by Qoy
it's *DESTRUCTOR* dammit!


errr.... at least I didn't write 'buy' for 'by' or something like that in the your/you're thread

quote:

]Original post by clabinsky
Hmm, no. Well, The structs BMP and SurfList are each included in a separate class. Both classes call their memberfunction Clear() in their respective destructors. The clear functions behave differently of course but the result is the same - it clears all outside references and resets the class to be used again or destroyed buy its destructor. The classes alse keep track of the "clear" status of the class to avoid repeated calls to Clear();



As long as you make sure that the BMP and SurfList structs are cleared before they are deleted, you should be ok. If I were you, I would make the structs handle their own cleaning up and clearing.

If it were me, then I would add a Clear function and destructor * ;p * to each of the structs.

*EDIT attempted to fix the face*

Invader X
Invader's Realm

Edited by - Invader X on December 23, 2001 4:12:22 AM

Edited by - Invader X on December 23, 2001 4:12:52 AM
clabinsky
clabinsky
quote:

As long as you make sure that the BMP and SurfList structs are cleared before they are deleted, you should be ok. If I were you, I would make the structs handle their own cleaning up and clearing.

If it were me, then I would add a Clear function and destructor * ;p * to each of the structs.



But is

delete lpBMP; and
delete lpSurfList;

enough? Does it delete the lpBMP->m_cImage? I call delete on both the lpBMP->m_cImage and concludes with delete lpBMP now.

As for the second point - very interesting. I have obviously missed this but will look into it further - thanks!


Why make it simple when you can make it sooo nice and complicated?
Colin Jeanne
Colin Jeanne
I dont think that just doing delete lpBMP; would clear the pointers if it doesn''t have a destructor that clears the pointers itself or if that class that uses it doesn''t call clear on it.

If you dont want BMP and SurfList to have destructors then you must delete them like this:

Clear(lpBMP);
delete lpBMP;

Clear(lpSurfList);
delete lpSurfList;

The reason I would add clear and a destructor to BMP and SurfList is because if my destructor calls Clear then the classes that use the struct can delete them by only calling delete, which makes one less mistake for me to make since I wont have to remember to have the class call Clear() on it.
clabinsky
clabinsky
quote:
Thank goodness for malloc() and free().


Hehe, to tell you the truth, this is not a problem in C++. My code works just fine and I could use malloc and free anytime. I just do not want to mix the languages in the same class. Now, while C has ite moments and is a very powerful language I use C++ because I need the reusability without cut and paste.

Anyway, as I said this is not a big problem and could be sorted with delete just as easy as free.

***Edited: missed out on the quote***



Edited by - clabinsky on December 23, 2001 11:59:56 PM
Why make it simple when you can make it sooo nice and complicated?
Colin Jeanne
Colin Jeanne
quote:

Thank goodness for malloc() and free()



malloc() and free() would do nothing for the LPDIRECTDRAWSURFACE7 in SurfList so you cant just free the memory to the SurfList struct unless you want to cause problems.

Invader X
Invader''s Realm
Shannon Barber
Shannon Barber
If you allocate the memory in every struct''s construtor(s) and define a proper destructor for every struct, then when you delete the highest level one, all of it''s sub-children will be deleted automatically.

This is, quite possibly, the most useful enhancement made by C++ to C.

new calls the ctor
delete calls the dtor
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Stoffel
Stoffel
In my "UML for Embedded Systems" book I''m reading at home there was this quote:
"It is well-documented that the biggest problem with C++ is C."
synthexp
synthexp
clabinsky :

If I were you, I''d try stl::list or stl::vector. They do automatic deletion when you call stl::***::clear (), that is, they call the struct''s destructor.

I once tried using stl::vector on particle system instead of using th traditional linked list and I was amazed at the efficientness of stl::vector. I easily had an increased of over 20% in fps plus my algorithm is very much clearer.

For your bitmap problem, you could use stl::list since they are optimized for bidirectional usage. This means they can traverse the nod before and after in the most efficient way. If your algorithm requires random traversing, that is you''ll need to a few indexing m_lpNod->[x], use stl::vector.

You may have to scrap your algorithm quite a lot, but it''s worth the hassle.
Regards,synthexp
synthexp
synthexp
I have to add this too, when using stl::vector or stl::list, you don''t have to pay much attention to individual node in terms of cleaning the object. When you call ::clear (), it will automatically calls the destructor of each object in the list. If you have 2,000 objects in the list, it will call each of the object''s destructor.

Example :
for (i = 0, i < 2000, i++)
{
m_vNode.push_back (T_OBJECT (arg1, arg2)); // calls object constructor and push it into the vector stack.
}

// End of prog
m_vNode.clear ();
Regards,synthexp

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.