For small procedures we can prevent memory leaks in case of exception in such way:
proc() {
//allocate memory for matrix
try {
}
catch {
//free matrix memory
}
...
//free matrix memory
}
In case if our procedure is more complicated:
proc() {
//allocate memory for matrix
try {
}
catch {
//free matrix memory
}
...
try {
}
catch {
//free matrix memory
}
...
try {
}
catch {
//free matrix memory
}
...
//free matrix memory
}
It looks some clumsy. Is a better way, better programming style for memory leaks control exist? As far as I know, C++ has auto_ptr and we can develop procedures without any care about memory deallocation.
proc() {
//allocate auto_ptr
try {
}
catch {
}
...
}
But, as far as I know, auto_ptr isn't intended even for arrays. So, it isn't acceptable way in general case.