Skip to content
Tcl Source Code View Ticket
Login

View Ticket

invalid refcount for classes in itcl?

Closed70944d8f141bfe849c6bd37ded7ce04ce8e27052Created 6 years agoUpdated 6 years ago
Descriptionplain2020-09-28 13:28:16
We have multiple TCL interpreters running in different threads. We are suffering from a crash that seems to be caused by the access to the already deleted ItclClass. Long story short, itcl tries to delete the same class twice - when another thread interferes, it can allocate a new class at the same address overriding the ITCL_CLASS_IS_FREED flag which leads to a crash when itcl access this object for the second time.
Consider the following snippet (crash.tcl):

package require Iwidgets
iwidgets::Labeledwidget  .l
pack .l
after 1000
exit

I ran it like this (using the self-built interpreter to remove our multi-threaded environment from investigation): 
TCL_FINALIZE_ON_EXIT=1 tclsh8.6 ~/scripts/crash.tcl

I've also added some logging to ItclReleaseClass():

if (iclsPtr->refCount-- <= 1) {
    pthread_t id = pthread_self();
    fprintf(stderr, "    ItclReleaseClass: %p, refCount: %d, thread: %lu\n", iclsPtr, iclsPtr->refCount, id);
    ItclFreeClass((char *) clientData);
}

Now, in the logs I see:
    ItclReleaseClass: 0x6290004dba10, refCount: 0, thread: 140564963183552
    ItclReleaseClass: 0x6290004dba10, refCount: -1, thread: 140564963183552

This refcount -1 looks very suspicious meaning that class has already been freed. In a single-threaded environment there are no (re)allocations at this point of time => flags are not overwritten => the code works. However, it doesn't seem right to access the freed memory (without the custom allocator, that would be a sigsegv, but TCL allocator helps here preventing segmentation violation).
dgpplain2020-09-28 13:38:21
Please report precise versions of all packages in use.
ivk Ticket [70944d8f14]: 3 changes [History diff]2020-09-28 14:01:52
dgpplain2020-09-28 14:18:28
Can the problem be demonstrated without use of Itk and Iwidgets?
ivkplain2020-09-28 14:45:02
> Can the problem be demonstrated without use of Itk and Iwidgets?

Not that I am aware of - this is already a reduced example, original script is several thousands lines of code.
I have debugged the itcl code to what I think is the problem - e.g. the refcounting issue. It appears that the number of pointers to the class does not match the actual reference count, but my knowledge of tcl code is very poor and I cannot fix that myself.
ivkplain2020-10-01 09:15:31
I do see the same output with the simple hello world:

package require Itcl
itcl::class helloworld {
  public variable owner "No-one"

  method greet {} { puts "Hello World from $owner" }
}

helloworld  h1
h1 greet

This example doesn't crash in the multi-threaded environment, though.
sebreswiki2020-10-02 13:16:47

This is surely an Itcl issue, so I opened new ticket there - itcl/f0a72b44d7.

Confirmed (and fixed there).

Thus closed here.

Log in (anonymous is fine) to add a comment.