Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
What happens when we allocate through New and deallocate through free.
Though its illegal to use and we will get a server crash.
I just want to know how the steps/process leads to server crash
Posted

For normal basic types (int, float, ...) I would assume nothing too important should happen.

For classes (and structs) it will not call the class destructor, and all memory allocated by the class will not be released (if done properly in the destructor) and some clean up code might not be called if implemented in the destructor.

If this is your case, then the "server" will probably leak memory during it execution and will crash if it comes to a point where it consumes all the memory on the system.
 
Share this answer
 
Comments
Chuck O'Toole 4-Nov-11 10:33am    
Good point on the class destructor. free() will never deal with that. +5
prasadvgp 14-Nov-11 7:24am    
Thanks@Maximilien
there will be default destructor which will be created by compiler rt, if we dont create a destructor. is this something different?
Don't do it! If you do have code that manages memory allocation this way then you are heading for trouble, and probably unemployment.
 
Share this answer
 
Comments
Chuck O'Toole 4-Nov-11 10:36am    
Second this statement. new() / delete() are mated. So are malloc() / free(). Never cross-pollinate your memory calls.
Andrew Brock 5-Nov-11 4:20am    
5'd. Microsoft's implementation uses malloc/free as the underlying code, but good advice. This cannot be guaranteed with other compilers/libraries
Richard MacCutchan 5-Nov-11 4:47am    
Thanks. I know how new/delete actually works under the covers, but explaining that to someone who does not quite understand this mechanism is likely to lead them down the wrong road.
This is a good question.

Like Maximilien said, you will delete the memory directly associated with the pointer you are using with the call to free, however, if the pointer is to an object, the destructor will not be called, and the memory inside of the destructor that is released will cause a leak.

The most likely cause of a crash in your server, is the run-time library that is used in your call to new, is using a different memory allocation function underneath that is not matched to your call to free.

Two examples would be,
1) If new was implemented using the Windows API call ::HeapAlloc.
2) If new uses ::malloc to allocate memory from a different C run-time library than the library you are linking in for the call to free.

You should always call the matching "release" memory method with each allocation.
You can mix malloc/free and new/delete allocations in your code, however, the memory management for each allocated buffer must use the matching release function.
 
Share this answer
 

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