I am wondering if there are any rules/good practices I should follow for avoiding memory leaks.
Now and then I am developing some useless app for R5/Haiku (which I never finish by the way :-)), and every time I encounter problems and questions concerning memory management.
Are there any rules for stack allocation vs heap allocation?
For instance when sending a message, is it better to allocate it on the stack:
BMessage msg(B_SOME_CONSTANT);
BMessenger->SendMessage(&msg);
or do I allocate it on the heap?
BMessage *msg = new BMessage(B_…);
BMessenger->SendMessage(msg);
delete msg;
Or doesn’t this matter at all?
I am also wondering if objects like a BStringView make a copy of the data it’s arguments make a reference to:
//Allocate them on the heap
char* name = “newbie”;
char* title = “question”;
//pass them to the stringview
BStringView(BRect(0,0,50,20), name, title);
//Is it allowed to delete them??
delete name;
delete title;
Is the above allowed, or do I need to preserve the allocated Strings?
If you’re thinking why I am not trying it out; i’m at work and want to know now
These questions may be a little too much about C++ in general, but I can imagine they are Beos/Haiku specific too.
Are there any facilities in the OS to assist in memory management?