Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!ucbvax!nj From: nj@magnolia.Berkeley.EDU (Narciso Jaramillo) Newsgroups: comp.sys.amiga.programmer Subject: Re: Message Ports Message-ID: Date: 26 May 91 05:41:15 GMT References: Sender: nobody@ucbvax.BERKELEY.EDU Distribution: comp Organization: Postcarcinogenic Bliss, Inc. Lines: 82 In-reply-to: bart@asgard.pub.uu.oz.au's message of 25 May 91 15:09:09 GMT In article bart@asgard.pub.uu.oz.au (John Butcher) writes: > Ive been trying to do message ports, but I havent had lots of success, my > ports wont get properly recognized by name. Below small test program I > wrote : > main( argc, argv ) > int argc; > char *argv[]; > { > struct MsgPort *mp; > mp = FindPort( argv[1] ); > if ( mp == NULL ) /* No port with that name */ > { > mp = CreatePort( argv[1], 0 ); /* so create one ! */ > printf("Created \"%s\" at 0x%0x\n", argv[1], mp ); > } > else /* found the given port */ > { > printf("Found \"%s\" at 0x%0x\n", argv[1], mp ); > DeletePort( mp ); /* so remove it */ > } > } > 2.RAMB0:> port crud > Created "crud" at 0x208d28 Yay, it created ok !! > 2.RAMB0:> port anticrud > Found "anticrud" at 0x208d28 Whats this, I thought I called you crud ! What's probably happening is that CreatePort doesn't copy the string you pass to it. When you create "crud", mp->mp_Node.ln_Name gets argv[1], which currently points to "crud." Your program then exits. When you invoke the program again, argv[1] happens to be in the same place, and so mp->mp_node.ln_Name points to whatever name you put on the command line in the second invocation. This makes the name of the message port magically change to the name you give it the second time around (i.e. the new argv[1]). So the program thinks the port name is the same as the one you passed, and it finds it. The only way around this that I can think of is to AllocMem space for a new string when you create a port, but don't free it when you exit; free it only after you delete the port the next time you invoke the program with that port name. (If you AllocMem some memory in one program, can you FreeMem it in another? I assume so, as long as it's MEMF_PUBLIC.) In other words: -- void main(int argc, char *argv[]) { struct MsgPort *mp; char *new; mp = FindPort(argv[1]); if (mp == NULL) { new = AllocMem(strlen(argv[1])+1, MEMF_PUBLIC); /* need 1 byte for \0 */ if (!new) { die_horribly(); } strcpy(new, argv[1]); mp = CreatePort(new, 0); printf("Created \"%s\" at 0x%0x\n", new, mp); } else { printf("Found \"%s\" at 0x%0x\n", argv[1], mp); Forbid(); /* just in case--may not be necessary */ FreeMem(mp->mp_Node.ln_Name, strlen(mp->mp_Node.ln_Name)+1); DeletePort(mp); Permit(); } } -- That solution is kind of gross, but then again I don't think ports were meant to hang around after a program exited. Do you really need a port that lasts after your program's termination, or was this just for the test program? nj