Spot the memory leak

Started by
14 comments, last by mfawcett 18 years, 10 months ago
Quote:
You quoted me so I assume you read what I typed, but apparently you missed some vital parts of my statements, especially the ones where I mentioned overloading operator new for the Collision_Relay class, or even the global operator new. Go back and reread and you'll see why your response has left me baffled.


What I find baffling is how you seem to think one cannot use an overloaded operator new without using placement new or specifically implementing it for a class. Again, I assure you that you are wrong. The code reproduced above could easily be using a custom new implementation with all the gory details behind the scenes.

Quote:I know exactly what happens in the event of an unhandled exception. Your previous post was talking about assuming the user handled exceptions, which, I will re-iterate, has absolutely nothing to do with the problem(s) at hand.


And I will re-iterate that the entire discussion has nothing to do with the problem(s) at hand. The OP asked for help spotting a memory leak and received a response of "don't check for NULL returning from new because I say you shouldn't". My responses have been to illustrate that the assumptions behind that statement are flawed, and that when a person asks for help with a particular area of their code time is not best spent critiquing minor details that one may not even fully understand. Instead, why not look at the problem the person is asking about.

Advertisement
Quote:Original post by Tesseract_Hotplate
What I find baffling is how you seem to think one cannot use an overloaded operator new without using placement new or specifically implementing it for a class. Again, I assure you that you are wrong. The code reproduced above could easily be using a custom new implementation with all the gory details behind the scenes.


Okay, now I see the problem. Your reading comprehension failed, so we had a misunderstanding. I'll re-quote what I originally typed for you, and then expound upon it to perhaps clear up your [mis]-understanding. Is English not your first language perhaps?

Here are my original sentences, with further explanations.

Quote:The first assumption seems reasonable.

This sentence means that I believe that it is safe to assume that the OP is not using a custom memory manager.

Quote:The keyword 'new' is clearly used, and unless the person overloaded operator new for the Collision_Relay class, or global operator new, then he obviously can't be using his own memory manger, because placement new isn't being called.

I think this is the sentence you are having trouble with. I list out the facts, assumptions, and conclusion in one sentence. Given the confusion this has caused upon you it seems I should've broken this into parts.

1. The keyword 'new' is being used (it is evident in his code).
2. The OP does not mention overloading operator new for the Collision_Relay class.
3. The OP does not mention overloading global new (or redefining the keyword ala MFC).
4. placement new is clearly not being used.

Quote:I think, given the nature of the question, that it is a very reasonable assumption, and one that needed no clarification.

I then re-iterate the conclusion: The OP is using the plain old 'new' operator.


Quote:Original post by Tesseract_Hotplate
And I will re-iterate that the entire discussion has nothing to do with the problem(s) at hand. The OP asked for help spotting a memory leak and received a response of "don't check for NULL returning from new because I say you shouldn't". My responses have been to illustrate that the assumptions behind that statement are flawed, and that when a person asks for help with a particular area of their code time is not best spent critiquing minor details that one may not even fully understand. Instead, why not look at the problem the person is asking about.


Certainly, that is why I mentioned that although 'new' should throw, a lot of older compilers don't behave that way, and NULL could certainly be returned. I think it might help if you don't skim my responses, and instead, please take the time to actually read them.
--Michael Fawcett
Quote:Okay, now I see the problem. Your reading comprehension failed


Come now. Just because I've demonstrated a greater understanding of the use of overloaded operators in C++ than you, you resort to name calling?

Perhaps that shouldn't be surprising, since you only joined this conversation to argue with me, not to help the OP.

Quote:This sentence means that I believe that it is safe to assume that the OP is not using a custom memory manager.


Then that is the end of the discussion. Because you admit that it is an assumption. Furthermore, this assumption was used to make a point completely irrelevant to the topic at hand.

Quote:
1. The keyword 'new' is being used (it is evident in his code).

- Which means nothing.
Quote:
2. The OP does not mention overloading operator new for the Collision_Relay class.

- Which means nothing. You should really get a C++ book.
Quote:
3. The OP does not mention overloading global new (or redefining the keyword ala MFC).

- See previous
Quote:
4. placement new is clearly not being used.

- And again with the placement new. Seriously, read a C++ book.

Quote:I then re-iterate the conclusion: The OP is using the plain old 'new' operator.


I then reiterate my conclusion: you do not know what the OP is using, and advising the user regarding how to handle return values from 'new' is both pointless and presumptive. Quick, tell me how deleting the line that checks the return value from 'new' will resolve the memory leak? I'll wait.

Obviously you are just one of the many amateur coders who loves to hang out on boards, wait for someone to post code with a problem, and then pick it apart because it doesn't match your style or approach. Which is why people don't ask questions. Your type are actually detrimental to the whole process of sharing information and resolving problems.
Sorry for derailing the thread. I assure you I am not amateur, and if you go through my past postings, you'll find I am extremely knowledgable about memory management and operator overloading, and try to be very helpful on these boards.

Could you kindly post an example of how the OP's code could possibly be using a custom memory manager? Remember, he hasn't overload the Collision_Relay class's operator new, he hasn't overloaded global operator new, he hasn't redefined the new keyword, and he isn't using placement new. I eagerly await your response.
--Michael Fawcett
Quote:Could you kindly post an example of how the OP's code could possibly be using a custom memory manager? Remember, he hasn't overload the Collision_Relay class's operator new, he hasn't overloaded global operator new, he hasn't redefined the new keyword, and he isn't using placement new. I eagerly await your response.


This is, obviously, a pointless argument. However I find it very interesting that you don't understand the problem with your statements.

All you have seen is a code snippet. You do not have the entire program before your eyes. From this code snippet you draw all the above conclusions, all of which could very easily be false. Whether or not the OP later confirmed them is irrelevant. At the time you - and the person who first critiqued checking the return value from new - made your assumptions, you did not have such confirmation of your assumptions.

One could just as easily state that the person's program doesn't work because he didn't define the class Collision_Relay, or Collision_Handler. Or because his entire engine appears to be one function. Both this pointless observation and the other pointless observation add nothing to the discussion, and require limiting thought to the code sample given.

In any case, since you asked:

In some file separate from code snippet:
inline void *operator new(unsigned int uiBytes, const char *pszFile, s32 iLine){    // Implementation ...}inline void *operator new[](unsigned int uiBytes, const char *pszFile, s32 iLine){    // Implementation ...}#define new	new(__FILE__, __LINE__)


The code snippet above would then work, perfectly, using the new operator exactly as if it hadn't been overloaded.

The point being, you don't know what is going on behind the scenes of the code. Furthemore, the posted 'advice' was not even pertinent to the question being asked.

More than anything this reminds me of the people on comp.lang.c who needlessly pick apart code which is posted by the author to find some problem. Newbies will post their 'void main' functions with an obvious bug inside, and get 100 posts telling them 'void main is wrong, idiot, it doesn't exist' but not a single reply addressing their question.
You spouted off insults about how you knew more about operator overloading, and how I was clearly wrong, yet all of your posts reiterate things I've said in my own posts, only you quote mine as wrong. It's very disconcerting to see someone so vehemently defend himself with things like:

Quote:Original post by Tesseract_Hotplate
Quote:Original post by mfawcettThe first assumption seems reasonable. The keyword 'new' is clearly used, and unless the person overloaded operator new for the Collision_Relay class, or global operator new, then he obviously can't be using his own memory manger, because placement new isn't being called. I think, given the nature of the question, that it is a very reasonable assumption, and one that needed no clarification.

You honestly think that if you use the keyword 'new', you can't be using your own implementation? I assure you that you are wrong. 'new' is an operator which can be overloaded.

I mean, that is baffling. Where in my quote did you possibly get the idea that I did not know 'new' was an operator that could be overloaded. Was it perhaps me mentioning every way 'new' could possibly be replaced? I was literally dumbfounded by your reply, which is why I figured the only way that you could've reached that conclusion was if you misunderstood, if you skimmed, or if English was not your first language.

Later I asked you, please, pray tell, how is it possible to be using a custom memory manager when Collision_Relay class did not overload operator new, the global operator new was not overloaded, he did not redefine the new keyword, and did not use placement new. You then posted code to say that indeed it is possible, and that I am horribly mistaken, and that I should go read a C+ book. I was hoping for enlightenment, perhaps I'd learn something new, but your code 1. overloads the global operator new, and 2. redefines the new keyword.

All of my posts have said that yes, it is an assumption we made that the user was not overloading operator new (and I hope I don't need to list all the ways that that is possible again), however an assumption that I believe can be safely made in this context. The OP came to the forum asking about a memory leak. It seems like common sense that if you made your own memory manager, and are then having memory leak problems, you better mention to us that you are using a custom memory manager.

As the case may be, the user
1. Did not mention overloading Collision_Relay class's operator new
2. Did not mention overloading the global operator new
3. Did not mention redefining the new keyword
4. Did not use placement new

Perhaps before giving advice next time, we should ask the OP if he is using his own implementation of the STD library as well? I mean, let's not make assumptions, right?
--Michael Fawcett

This topic is closed to new replies.

Advertisement