Problem with Tcl_SetResult and Tclkit under Windows

72 views
Skip to first unread message

Gerard Durand

unread,
Apr 12, 2011, 10:18:10 AM4/12/11
to sta...@googlegroups.com
Hi,
I'm trying to return a long chain from C to Tcl.
If I use :
sprintf(interp->result,"%s",Message) ; with Message containing my character
chain, it works. But Message can not be more than 200 characters long
(TCL_RESULT_SIZE limit).
Normally the solution is to use Tcl_SetResult (and it will be the only one with
tcl 8.6 I think ! even for short strings) :
Tcl_SetResult(interp,Message,TCL_VOLATILE);

Like that, it works on Linux, it works too on Windows if my tcl code calls
directly my compiled C code inside a dll. But it does not work if my dll is
included in a kit (though the 1st solution with sprintf is OK, except when my
Message is too long of course).
I use tclkit and tclkitsh 8.5.9 on Windows XP and my compiler is gcc (TDM 4.5.2
version on Windows).
I have tried others solutions like :
Tcl_SetVar(interp,"v1_out",Message,TCL_GLOBAL_ONLY);
or :
Tcl_Obj * ResultObj ;
ResultObj = Tcl_NewStringObj(Message,-1);
Tcl_SetObjResult(interp,ResultObj);

It's always OK when my tcl code calls directly the dll, but never work when It's
inside a kit. I obtain this error :
Microsoft Visual C++ Runtime Library
Runtime Error !
This application has requested the Runtime to terminate it in an unusual way, ...

Apparently, I have not such problems under Linux and tclkit (though versions are
a little bit different).

So, what is wrong ?
Thanks for your help.

Gerard


Harald Oehlmann

unread,
Apr 12, 2011, 11:16:52 AM4/12/11
to Gerard Durand, sta...@googlegroups.com

Hi Gerard,

I think the 200 characters limit is due to sprintf.

The crash is weired, anyway. At least, the Obj method looks very clean
to me. When the sprintf helps, there must be something with variable scope.
- Did you try to put "Message" (and the data it points to) in a global
variable (just for test) ?
- TCL_Stubs enabled ?

I have MS-VC++ Express installed. If something crashes, I have the
possibility to start the C development environment and trace back on the
stack to find the errorneous calling procedure.

If error happens also, if your DLL is not wrapped but you load it from a
tclkit, you may use MSVC++ Express to set a breakpoint in the dll and
use step-by step processing to eventually find the reason for the issue.

J'éspère que ca aide un peu,
Harald

Pat Thoyts

unread,
Apr 12, 2011, 11:39:13 AM4/12/11
to Gerard Durand, sta...@googlegroups.com
On 12 April 2011 15:18, Gerard Durand <gerard-flor...@orange.fr> wrote:
> Hi,
> I'm trying to return a long chain from C to Tcl.
> If I use :
> sprintf(interp->result,"%s",Message) ; with Message containing my character

Don't do that. Its been deprecated for about 12 years or more.

Tcl_SetObjResult(interp, Tcl_NewStringObj(Message, -1));
should be the way to do this. This should take a copy of the string
and allocate it into a new Tcl object. I'd suspect the other failures
are due to memory for the string being allocated by one heap while the
Tcl copy used another heap.

Make sure that your extension DLL is compiled with TCL_STUBS and
linked to tclstub8[456].lib. Tclkit cannot work with non-stubs linked
extensions.

Gerard Durand

unread,
Apr 12, 2011, 1:22:13 PM4/12/11
to sta...@googlegroups.com
Pat Thoyts <patthoyts <at> gmail.com> writes:

>
> On 12 April 2011 15:18, Gerard Durand <gerard-florence.durand <at> orange.fr>


wrote:
> > Hi,
> > I'm trying to return a long chain from C to Tcl.
> > If I use :
> > sprintf(interp->result,"%s",Message) ; with Message containing my character
>
> Don't do that. Its been deprecated for about 12 years or more.
>

OK, but I have still many codes doing that ...


>
> Tcl_SetObjResult(interp, Tcl_NewStringObj(Message, -1));
> should be the way to do this. This should take a copy of the string
> and allocate it into a new Tcl object. I'd suspect the other failures
> are due to memory for the string being allocated by one heap while the
> Tcl copy used another heap.
>
> Make sure that your extension DLL is compiled with TCL_STUBS and
> linked to tclstub8[456].lib. Tclkit cannot work with non-stubs linked
> extensions.
>

That was the reason apparently. Now I have compiled and linked with TCL and TK
stubs and it works on a small test. Normally, it should also work on a bigger
code (I hope :-))

Thanks for the help.

Gerard

Kevin Kenny

unread,
Apr 12, 2011, 9:07:33 PM4/12/11
to sta...@googlegroups.com
On 04/12/2011 01:22 PM, Gerard Durand wrote:
> Pat Thoyts<patthoyts<at> gmail.com> writes:
>
>>
>> On 12 April 2011 15:18, Gerard Durand<gerard-florence.durand<at> orange.fr>
> wrote:
>>> Hi,
>>> I'm trying to return a long chain from C to Tcl.
>>> If I use :
>>> sprintf(interp->result,"%s",Message) ; with Message containing my character
>>
>> Don't do that. Its been deprecated for about 12 years or more.
>>
>
> OK, but I have still many codes doing that ...

Well, stop doing that. Really. It was deprecated in 1998.

--
73 de ke9tv/2, Kevin

Reply all
Reply to author
Forward
0 new messages