Archived

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

Scarface5013

free class* ??

Recommended Posts

quote:
Original post by Arild Fines
Hmm... they don''t teach English in German schools, do they?

Actually, they do.

Scarface, was mochtest du? Vielleicht ich kann dir hilfen... Meine deutsche ist nicht mehr so gut, aber ich glaube ich kenn (ja?) genug.

I wanna work for Microsoft!
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
ok guys, i know it''s unbeliveable but they ARE teaching english in german schools and university. but i''m not sure, if you heart something about sozial contacs and behavior. i''m so sorry, if my literary knowledge is not corresponding to yours. so don''t call me a fool for not knowing any vocabulary of informatic field. perhaps you can teach me something, i''m not sure, but i thought i''m here to do. first i thought i visit this forum to learn something about programming, but we can rename it in "GOTO english" or "english in 21 days".

but i''m getting of topic. so i want to explane my question in a proper way, in hope someone there will understand my foolish english.

i started to write my one gui in dx and therefor i declared a new class. this class owns to attributes of this class in a list. in code :

class CXWindow
{
CXWindow *child;
CXWindow *parent;
}

after some calls and adds the chil and parent is pointing to a list of objects. the next step i started is deleting some of these. so i have to free the memory. but i''m not sure how. i think i use a runpointer to the object i want to delete and call:
free( runpointer );
runpointer = NULL;
that''s right ??

i hope this way to explane my problem is more proper than the last one and no one is bored.

thx

Share this post


Link to post
Share on other sites
You tell ''em Scarface ^_^ People seem to bash each other/call each other newbie based on language problems *way* too often... I could understand your question immediatly, strange as though it might seem from the other replies. Can''t anwser it though. I''ll just lurk around ''till I see the anwser though ^_^

Share this post


Link to post
Share on other sites
Well how did you allocate the objects? Did you use new for this? I hope you didn''t use malloc.
So when using new to create an object simply use delete to destroy the object on the heap.

Share this post


Link to post
Share on other sites
OK....

  
class X
{
//stuff

};

void main()
{
// Set to NULL on initialising

X* pSomething = NULL;

// Creating the object

pSomething = new X;

// Destroying the object

delete pSomething;
pSomething = NULL;
}


For safely deleting objects, I made the following useful macro:

  
#define DELETENULL(obj) { if (obj) { delete [] (obj); (obj) = NULL; } }


Hope this helps This is C++ only though. In C you don''t have new/delete, so you would use the old-school malloc/free

Share this post


Link to post
Share on other sites
quote:
Original post by dusik
For safely deleting objects, I made the following useful macro:


#define DELETENULL(obj) { if (obj) { delete [] (obj); (obj) = NULL; } }


What does the "if (obj)" test add? If you call delete or delete[] with a null pointer, the result is a no-op.



--
Very simple ideas lie within the reach only of complex minds.

Share this post


Link to post
Share on other sites
quote:
Original post by Scarface5013
yes i used new for this. so the answer seem to be delete.
and the pointer ??


A better method would be to use a smart pointer with an appropriate ownership policy. For example, if the class is the sole owner of the object pointed to, then auto_ptr<> would be suitable. You would set the auto_ptr during object initialisation, and deletion of the pointed-to data would be done automatically when the owner object is deleted. If auto_ptr''s ownership is unsuitable, then you can find more smart pointers at www.boost.org.

--
Very simple ideas lie within the reach only of complex minds.

Share this post


Link to post
Share on other sites
I believe the "if( obj )" statement first tests to see if the object isn''t NULL. If it is null, it returns false and the next statement is skipped. Otherwise, it goes ahead and frees the class.

Share this post


Link to post
Share on other sites
quote:
Original post by Sethius
I believe the "if( obj )" statement first tests to see if the object isn''t NULL. If it is null, it returns false and the next statement is skipped. Otherwise, it goes ahead and frees the class.


Yes, I know that, but it adds nothing. As I said, calling delete on a null pointer is a no-op, so the check is redundant.



--
Very simple ideas lie within the reach only of complex minds.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
i think it makes sense to use that macros - if U use it all the time...

actually i''m using:
#define SAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } }
#define SAFE_DELETE_ARRAY(p) { if(p) { delete[] (p); (p)=NULL; } }
#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }

if U make a programming mistake and delete an object twice, the compiler will throw the code away and nobody cares...

hope my english is good enough 2 undaztand...
(yeah, they teach English in school)

Share this post


Link to post
Share on other sites
There is no need to check to see if the pointer is not NULL before freeing it, that''s the point. It makes absolutely no difference. It makes a difference for Release() calls on COM objects, but not on freeing memory. And you shouldn''t call delete [] by default (as seemed to be suggested by one poster). You only use the [] when you''re sure you''re deleting an array.

Personally, there are times when I don''t want the pointer set to NULL, because I would like to be made aware of ''double-frees'' so that I can fix them. (As they usually signify a logic error somewhere.) Other times, I set a pointer to NULL because NULL is a valid value. So rather than always using a given macro, I do whatever is needed for the given situation.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post


Link to post
Share on other sites
Personally, I don''t agree it''s a good idea to use macros for this. Macros have proven to be problematic for many programmers. Even Stroustrup recommends to avoid them wherever there is an alternative. In this case, there is indeed an alternative. An interesting option would be to utilise policy classes as is done in the Loki library. It''s a simple matter to write creation and deletion policies. Another option here is to use smart pointers to avoid explicitly having to code the delete statement.

quote:

if U make a programming mistake and delete an object twice, the compiler will throw the code away and nobody cares...



Is there something about "calling delete on a null pointer is a no-op" that you didn''t understand?

Besides that, I find the idea of letting a "programming mistake" go undetected to be somewhat contentious. I''d rather have a crash in debug mode so that I could spot the mistake and sort out the object''s ownership issues.

--
Very simple ideas lie within the reach only of complex minds.

Share this post


Link to post
Share on other sites