Thread: Re: [tcljava-user] (no subject)
Brought to you by:
mdejong
From: Dan D. <dda...@ya...> - 2008-09-25 12:00:51
|
Martin and Mo, Thank you both for quick responses. The reason I was building up the TclList then just returning one item was because of a design decision to have lower layers build the list, then an upper/interface layer decide if we truly needed a list returned or just one item. I will take your advice (which I figured out on my own yesterday) and instead have the lower layers build a Java List, then the upper layer can decide if we need to return a TclList or just a TclObject and convert accordingly. I did read yesterday that when a list is released it releases ALL its contents too. I expected it to be more like Java where if there are still references to the members they would be left alone. I figured adding list.preserve() to prevent the crash was a memory leak, that's why I asked in the first place, I didn't want to leave the "fix-bug" in my code. :) Why does this not happen when the list is made up of just non-list TclObjects? Thanks again, Dan ----- Original Message ---- From: Martin Lemburg <mar...@gm...> To: A list for users of tcljava <tcl...@li...>; dda...@ya... Sent: Thursday, September 25, 2008 4:42:37 AM Subject: Re: [tcljava-user] (no subject) Hi Dan, the reason is, that you return an element from the just built list by pushing it into the interpreter. If the method is ending the list object is released and all its elements will be released, too. So the result list element won't be present anymore. In my eyes even a "list.preserve" call wouldn't solve the problem because it raises the question, when the list would be release again? I would say ... never! So I would duplicate the object I want to return! In C(++) I would do: Tcl_Obj* pList = Tcl_NewObj(); // building the list Tcl_Obj* pElement = NULL; Tcl_ListObjIndex(pInterp, pList, nIndex, &pElement); Tcl_SetObjResult(pInterp, Tcl_DuplicateObj(pElement)); By the way ... why do you build a tcl list to extract one element and to forget it? Why not using Java container objects, if container objects are really needed? Ok - I hope my suggestion helps. Best regards, Martin Lemburg Dan Diolosa wrote: Hi, I am wondering why I need to "preserve" a TclList that I don't return to TCL, nor need/use after the function returns. I am trying to return just one sub-list from a list of sub-lists. If I have just a list of normal TclObjects I can return one item without a problem. What is wrong with this: public void cmdProc(Interp interp, TclObject argv[]) throws TclException { TclObject list = TclList.newInstance(); //Create the first sublist TclObject list1 = TclList.newInstance(); TclList.append(interp, list1, TclString.newInstance("1.1")); TclList.append(interp, list1, TclString.newInstance("1.2")); //Create the second sublist TclObject list2 = TclList.newInstance(); TclList.append(interp, list2, TclString.newInstance("2.1")); TclList.append(interp, list2, TclString.newInstance("2.2")); //Add to main list TclList.append(interp, list, list1); TclList.append(interp, list1, list2); //list.preserve(); //return just the second sublist list interp.setResult(TclList.index(interp, list, 0)); } When I run it (without the preserve()), I get this: % ls_test alloc: invalid block: 009B3DF0: f8 0 0 This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information. D:\ When I run a similar situation in my production code I get the error from JRE side, SIGSEGV on Linux and ACCESS_VIOLATION on Windows: # # An unexpected error has been detected by HotSpot Virtual Machine: # # SIGSEGV (0xb) at pc=0x4008eb78, pid=4098, tid=1074768448 # # Java VM: Java HotSpot(TM) Client VM (1.5.0_06-b05 mixed mode, sharing) # Problematic frame: # C [libtcl8.4.so+0x77b78] # # An error report file with more information is saved as hs_err_pid4098.log # # If you would like to submit a bug report, please visit: # http://java.sun.com/webapps/bugreport/crash.jsp # Aborted (core dumped) Thanks, Dan ________________________________ ------------------------------------------------------------------------- This SF.Net email is sponsored by the Moblin Your Move Developer's challenge Build the coolest Linux based applications with Moblin SDK & win great prizes Grand prize is a trip for two to an Open Source event anywhere in the world http://moblin-contest.org/redirect.php?banner_id=100&url=/ ________________________________ _______________________________________________ tcljava-user mailing list tcl...@li... https://lists.sourceforge.net/lists/listinfo/tcljava-user -- ________________________________ Martin Lemburg Emdener Strasse 19A D-10551 Berlin Private: Phone: +49 30 39 03 72 05 Mobile: +49 179 395 40 95 Fax: +49 1805 40 02-26 86 05 Email: mar...@gm... Office: Phone: +49 30 46 77 75-22 Fax: +49 30 46 77 75-11 Email: mar...@si... ________________________________ "We are all individualists!" "No, Im the only non-individualist!" (Monty Pythons "Life of Brian") ________________________________ |