6 added 304 characters in body edited Sep 22 '18 at 11:34 Szabolcs 172k1818 gold badges470470 silver badges10061006 bronze badges I am using a library that has functions like createSomeObject[] and deleteSomeObject[obj]. It is an interface to a C language library and it reflects how the C library is designed. How can I make sure that when I have code similar to Module[{obj}, obj = createSomeObject[]; (* some code here *) blackBoxFunction[]; (* some code here *) deleteSomeObject[obj]; (* ... *) ]  then the objects will always be safely cleaned up, even if the computation is aborted in the middle of the Module or blackBoxFunction[] does something unexpected (Throw[] or something else I didn't think of)? In C++ we have RAII for this. What is the most usual / idiomatic way to do safe resource management this in Mathematica? Note that even when only using built-in functions, this problem might come up with e.g. opening and closing file streams (it is very important to close streams). Even some packages shipping with Mathematica, such as TetGenLink, require explicit resource management. Finally, is it possible to design such libraries in a way that avoids explicit deleteSomeObject[obj] calls? Is it possible to integrate the library with Mathematica's built-in reference counting and garbage collection? I found the "RemoveSymbol" handler (see InternalHandlers[]), but it only seems to trigger on explicit Remove[], not when Temporary symbols (such as Module variables) are cleaned up. Update: If you need this specifically for a LibraryLink library in Mathematica 10.0, look up Managed Library Expressions. This feature was not yet available when I wrote the question. I am using a library that has functions like createSomeObject[] and deleteSomeObject[obj]. It is an interface to a C language library and it reflects how the C library is designed. How can I make sure that when I have code similar to Module[{obj}, obj = createSomeObject[]; (* some code here *) blackBoxFunction[]; (* some code here *) deleteSomeObject[obj]; (* ... *) ]  then the objects will always be safely cleaned up, even if the computation is aborted in the middle of the Module or blackBoxFunction[] does something unexpected (Throw[] or something else I didn't think of)? In C++ we have RAII for this. What is the most usual / idiomatic way to do safe resource management this in Mathematica? Note that even when only using built-in functions, this problem might come up with e.g. opening and closing file streams (it is very important to close streams). Even some packages shipping with Mathematica, such as TetGenLink, require explicit resource management. Finally, is it possible to design such libraries in a way that avoids explicit deleteSomeObject[obj] calls? Is it possible to integrate the library with Mathematica's built-in reference counting and garbage collection? I found the "RemoveSymbol" handler (see InternalHandlers[]), but it only seems to trigger on explicit Remove[], not when Temporary symbols (such as Module variables) are cleaned up. I am using a library that has functions like createSomeObject[] and deleteSomeObject[obj]. It is an interface to a C language library and it reflects how the C library is designed. How can I make sure that when I have code similar to Module[{obj}, obj = createSomeObject[]; (* some code here *) blackBoxFunction[]; (* some code here *) deleteSomeObject[obj]; (* ... *) ]  then the objects will always be safely cleaned up, even if the computation is aborted in the middle of the Module or blackBoxFunction[] does something unexpected (Throw[] or something else I didn't think of)? In C++ we have RAII for this. What is the most usual / idiomatic way to do safe resource management this in Mathematica? Note that even when only using built-in functions, this problem might come up with e.g. opening and closing file streams (it is very important to close streams). Even some packages shipping with Mathematica, such as TetGenLink, require explicit resource management. Finally, is it possible to design such libraries in a way that avoids explicit deleteSomeObject[obj] calls? Is it possible to integrate the library with Mathematica's built-in reference counting and garbage collection? I found the "RemoveSymbol" handler (see InternalHandlers[]), but it only seems to trigger on explicit Remove[], not when Temporary symbols (such as Module variables) are cleaned up. Update: If you need this specifically for a LibraryLink library in Mathematica 10.0, look up Managed Library Expressions. This feature was not yet available when I wrote the question. 5 replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/ edited Apr 13 '17 at 12:56 I am using a library that has functions like createSomeObject[] and deleteSomeObject[obj]. It is an interface to a C language library and it reflects how the C library is designed. How can I make sure that when I have code similar to Module[{obj}, obj = createSomeObject[]; (* some code here *) blackBoxFunction[]; (* some code here *) deleteSomeObject[obj]; (* ... *) ]  then the objects will always be safely cleaned up, even if the computation is aborted in the middle of the Module or blackBoxFunction[] does something unexpected (Throw[] or something else I didn't think of)? In C++ we have RAII for this. What is the most usual / idiomatic way to do safe resource management this in Mathematica? Note that even when only using built-in functions, this problem might come up with e.g. opening and closing file streams (it is very important to close streams). Even some packages shipping with Mathematica, such as TetGenLink, require explicit resource management. Finally, is it possible to design such libraries in a way that avoids explicit deleteSomeObject[obj] calls? Is it possible to integrate the library with Mathematica's built-in reference counting and garbage collection? I found the "RemoveSymbol" handler (see InternalHandlers[]see InternalHandlers[]), but it only seems to trigger on explicit Remove[], not when Temporary symbols (such as Module variables) are cleaned up. I am using a library that has functions like createSomeObject[] and deleteSomeObject[obj]. It is an interface to a C language library and it reflects how the C library is designed. How can I make sure that when I have code similar to Module[{obj}, obj = createSomeObject[]; (* some code here *) blackBoxFunction[]; (* some code here *) deleteSomeObject[obj]; (* ... *) ]  then the objects will always be safely cleaned up, even if the computation is aborted in the middle of the Module or blackBoxFunction[] does something unexpected (Throw[] or something else I didn't think of)? In C++ we have RAII for this. What is the most usual / idiomatic way to do safe resource management this in Mathematica? Note that even when only using built-in functions, this problem might come up with e.g. opening and closing file streams (it is very important to close streams). Even some packages shipping with Mathematica, such as TetGenLink, require explicit resource management. Finally, is it possible to design such libraries in a way that avoids explicit deleteSomeObject[obj] calls? Is it possible to integrate the library with Mathematica's built-in reference counting and garbage collection? I found the "RemoveSymbol" handler (see InternalHandlers[]), but it only seems to trigger on explicit Remove[], not when Temporary symbols (such as Module variables) are cleaned up. I am using a library that has functions like createSomeObject[] and deleteSomeObject[obj]. It is an interface to a C language library and it reflects how the C library is designed. How can I make sure that when I have code similar to Module[{obj}, obj = createSomeObject[]; (* some code here *) blackBoxFunction[]; (* some code here *) deleteSomeObject[obj]; (* ... *) ]  then the objects will always be safely cleaned up, even if the computation is aborted in the middle of the Module or blackBoxFunction[] does something unexpected (Throw[] or something else I didn't think of)? In C++ we have RAII for this. What is the most usual / idiomatic way to do safe resource management this in Mathematica? Note that even when only using built-in functions, this problem might come up with e.g. opening and closing file streams (it is very important to close streams). Even some packages shipping with Mathematica, such as TetGenLink, require explicit resource management. Finally, is it possible to design such libraries in a way that avoids explicit deleteSomeObject[obj] calls? Is it possible to integrate the library with Mathematica's built-in reference counting and garbage collection? I found the "RemoveSymbol" handler (see InternalHandlers[]), but it only seems to trigger on explicit Remove[], not when Temporary symbols (such as Module variables) are cleaned up. 4 deleted 358 characters in body edited Jun 18 '14 at 21:17 Szabolcs 172k1818 gold badges470470 silver badges10061006 bronze badges I am using a library that has functions like createSomeObject[] and deleteSomeObject[obj]. It is an interface to a C language library and it reflects how the C library is designed. How can I make sure that when I have code similar to Module[{obj}, obj = createSomeObject[]; (* some code here *) blackBoxFunction[]; (* some code here *) deleteSomeObject[obj]; (* ... *) ]  then the objects will always be safely cleaned up, even if the computation is aborted in the middle of the Module or blackBoxFunction[] does something unexpected (Throw[] or something else I didn't think of)? In C++ we have RAII for this. What is the most usual / idiomatic way to do safe resource management this in Mathematica? Note that even when only using built-in functions, this problem might come up with e.g. opening and closing file streams (it is very important to close streams). Even some packages shipping with Mathematica, such as TetGenLink, require explicit resource management. Finally, is it possible to design such libraries in a way that avoids explicit deleteSomeObject[obj] calls? Is it possible to integrate the library with Mathematica's built-in reference counting and garbage collection? I found the "RemoveSymbol" handler (see InternalHandlers[]), but it only seems to trigger on explicit Remove[], not when Temporary symbols (such as Module variables) are cleaned up. Update I'd like to highlight InternalWithLocalSettings from Leonid's answer. This is what I ended up using the most often, despite some shortcomings. JavaBlock and NETBlock also rely on it. I am using a library that has functions like createSomeObject[] and deleteSomeObject[obj]. It is an interface to a C language library and it reflects how the C library is designed. How can I make sure that when I have code similar to Module[{obj}, obj = createSomeObject[]; (* some code here *) blackBoxFunction[]; (* some code here *) deleteSomeObject[obj]; (* ... *) ]  then the objects will always be safely cleaned up, even if the computation is aborted in the middle of the Module or blackBoxFunction[] does something unexpected (Throw[] or something else I didn't think of)? In C++ we have RAII for this. What is the most usual / idiomatic way to do safe resource management this in Mathematica? Note that even when only using built-in functions, this problem might come up with e.g. opening and closing file streams (it is very important to close streams). Even some packages shipping with Mathematica, such as TetGenLink, require explicit resource management. Finally, is it possible to design such libraries in a way that avoids explicit deleteSomeObject[obj] calls? Is it possible to integrate the library with Mathematica's built-in reference counting and garbage collection? I found the "RemoveSymbol" handler (see InternalHandlers[]), but it only seems to trigger on explicit Remove[], not when Temporary symbols (such as Module variables) are cleaned up. Update I'd like to highlight InternalWithLocalSettings from Leonid's answer. This is what I ended up using the most often, despite some shortcomings. JavaBlock and NETBlock also rely on it. I am using a library that has functions like createSomeObject[] and deleteSomeObject[obj]. It is an interface to a C language library and it reflects how the C library is designed. How can I make sure that when I have code similar to Module[{obj}, obj = createSomeObject[]; (* some code here *) blackBoxFunction[]; (* some code here *) deleteSomeObject[obj]; (* ... *) ]  then the objects will always be safely cleaned up, even if the computation is aborted in the middle of the Module or blackBoxFunction[] does something unexpected (Throw[] or something else I didn't think of)? In C++ we have RAII for this. What is the most usual / idiomatic way to do safe resource management this in Mathematica? Note that even when only using built-in functions, this problem might come up with e.g. opening and closing file streams (it is very important to close streams). Even some packages shipping with Mathematica, such as TetGenLink, require explicit resource management. Finally, is it possible to design such libraries in a way that avoids explicit deleteSomeObject[obj] calls? Is it possible to integrate the library with Mathematica's built-in reference counting and garbage collection? I found the "RemoveSymbol" handler (see InternalHandlers[]), but it only seems to trigger on explicit Remove[], not when Temporary symbols (such as Module variables) are cleaned up. 3 added 356 characters in body edited Jun 12 '14 at 19:51 Szabolcs 172k1818 gold badges470470 silver badges10061006 bronze badges 2 added 196 characters in body edited Feb 19 '12 at 17:08 Szabolcs 172k1818 gold badges470470 silver badges10061006 bronze badges Tweeted twitter.com/#!/StackMma/status/168348380995661826 occurred Feb 11 '12 at 14:59 1 asked Feb 10 '12 at 17:08 Szabolcs 172k1818 gold badges470470 silver badges10061006 bronze badges