Skip to main content
gamedev.net
🔒 Locked

reference counting and __FILE__

Started by Damocles 4 replies 600+ views
Original Post
Damocles
Damocles
I''m looking into adding a debug version reference counter into my programs. I want to have a simple routine that I can drop into any of my apps and still continue to use new/delete as normal, but output info to a log in the case of reference counting anomolies. I figure the best method would be to add a small function call to the constructor of the abstract base class from which all my other classes extend. The reference counting part is dead easy, but to assist me in tracking down the source of the error, I want to be able to record the cpp filename of each created object. The problem is, if the reference function is in the base object, then any __FILE__ macros will equate to the base object''s file, not the file of the new subclassed object. So is there any way in which I can get the function to obtain the filename of the new object being created without having to add a function call to every object''s constructor? I''m using VC6.
------------------------------------------[New Delta Games] | [Sliders]
twanvl
twanvl
What if you overload operator new and delete?

// some header

operator new(size_t size, const char* filename, unsigned int line)
{
// log filename and line number here

return new char[size];
}
#define new new(__FILE__,__LINE__)


[edited by - twanvl on March 9, 2004 9:53:15 AM]
Damocles
Damocles
While the new is potentially very good, I''m not sure how I''m going to go about overloading delete.

I just did a quick test and VC6 is moaning about there being no matching delete (because it isn''t overloaded).

I''m not sure how to "match" them. I tried the following:


void operator delete(void *v)
{
delete v;
}

#define delete delete();


but it''s not playing ball. How can I get the pointer from the call to delete to pass into the overloaded delete function?
------------------------------------------[New Delta Games] | [Sliders]
Qw3r7yU10p!
Qw3r7yU10p!
You''re not really allowed to redefine c++ keywords, although vc++ will let you. Just let that be warning enough. It''s not a good idea, it''s not portable, you''d probably get sacked from a decent job... etc.
atlnewbie
atlnewbie
nevermind

[edited by - atlnewbie on March 9, 2004 10:44:07 AM]
SiCrane
SiCrane
Try doing a web search for "Paul Nettle mmgr" or "midnight mmgr" Paul Nettle (who goes by the handle of midnight on flipcode.com) wrote a memory debugging system that uses tons of #defines and such to log and track memory allocations. It''s fairly well known, so you should be able to find people to explain any individual bits that seem murky.

Topic Locked

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

Sign in to reply to this topic.