Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I use MathLink to call Mathematica from C program. After any opening to Mathematica, I do close the connection. However, I don't know why some connections are still there. I've been debugging for days but couldn't find the point.

Thus, I want to ask if there is any way to close all connection before I open the new one? Seems like not an optimal solution but for now its ok for me.


share|improve this question


LinkClose[#] & /@ Links[]; 

It maps the list of all Links (Links[]) through LinkClose

share|improve this answer
Sorry, how to do it from C? – Loi.Luu Nov 13 '13 at 14:55
@Loi.Luu how about sending an MLPutFunction with an EvaluatePacket containing Quit[] to close your kernel? page 29 – PlaysDice Nov 13 '13 at 15:29
I want to close other connections which are supposed to be closed already. To close the current connection, I call the MLPutFunction(lp, Exit, 0L); statement. – Loi.Luu Nov 13 '13 at 15:34
@Loi.Luu good point. Is a 'simple' way to use MLPutFunction to get MMA to do a variant of the LinkClose map (above), but not drop the current Link ? – PlaysDice Nov 13 '13 at 15:43
@Loi.Luu. As written, yes, in MMA it closes all open 'Links' (incl JLinks etc). You may have to figure out whether the kernel can only close Links that are open to it (i.e not 'zombie' links left from other kernels) and how to specify the 'Link' you need to retain. If the one you need to preserve is the last 'Link' you could make the command 'LinkClose[#]&/@Drop[Links[],-1]' i.e 'LinkClose' the list of all 'Links' but omit the last one – PlaysDice Nov 13 '13 at 16:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.