Click here to Skip to main content
15,885,959 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,
Please see the below code and please tell me that i am doing perfectly right or there is some risk involve and what kind of risk?

C#
XGGraphicAttribute* XGGraphicAttribute::Create()
{
    return new XGGraphicAttribute;
}


Regards
BitsMax
Posted
Comments
Legor 29-Sep-11 7:41am    
I dont see any casting here.

Looking at the description of the new[^] operator, I would say you are doing it correctly. Since you are returning an object of the type that the Create() function says it will return, you will not need any casting within this code.
 
Share this answer
 
Comments
Albert Holguin 29-Sep-11 9:31am    
Correct, +5
Sergey Alexandrovich Kryukov 30-Sep-11 1:32am    
Yes, my 5, too.
--SA
If there is any risk it is that the caller of the function may not delete the returned object, creating a memory leak.
So document the function properly (which you should always do) and state that the caller is responsible for cleaning up the returned object.
 
Share this answer
 
v3
Comments
Albert Holguin 29-Sep-11 9:31am    
Yep, not a huge problem as long as the dev knows what he's doing but worth mentioning, +5
Sergey Alexandrovich Kryukov 30-Sep-11 1:33am    
Understanding is the best safety, my 5.
--SA
There is absolutely no casting required as new will return exactly the type you declared as return type. Maybe you are confusing this with malloc(), which returns a void* ?

The only thing I can think of that you might want to change, is catching possible exceptions from the constructor - if that is the purpose of your Create() function! You could for instance make a nothrow guarantee (see http://www.gotw.ca/gotw/059.htm[^]) like this:

C++
XGGraphicAttribute* XGGraphicAttribute::Create() throw() {
   XGGraphicAttribute* result = 0;
   try {
      result = new XGGraphicAttribute;
   }
   catch (std::bad_alloc) {
      // memory allocation failed
      // send some out-of-memory notification
   }
   catch () {
      // contructor failed
      // send some notification, if you want to
   }
   return result;
}


Then again, it might be better to just let those exceptions hit the caller instead; that way there is no danger that the caller never thinks to test the result value - if the exception remains uncaught, it will immediately catch the attention of the developer, and (hopefully) lead to a fix.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 30-Sep-11 1:32am    
Unconfusing, a 5. :-)
--SA
In fact, thnis is the contrary... Casting is dangerous because it rely on the programmer instead of the compiler.

Thus take the habit to never add a cast if it not strictly necessary and if it is necessary, then first check if it is possible to avoid the cast by correcting the code like changing a badly selected variable type for something more appropriate.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 30-Sep-11 1:31am    
Good point, a 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900