Xinus
Xinus

Reputation: 30513

Firefox crashes when XPCOM function called from thread

I want to create a thread from XPCOM Component ... Here is a code for that

nsresult rv = NS_OK;
    nsCOMPtr<Callback> obj = do_CreateInstance("@jscallback.p2psearch.com/f2f;1", &rv);
    NS_ENSURE_SUCCESS(rv, rv);
    char* str="Hello from C++";
    obj->Status(str);
    _beginthread( (void(*)(void* ))&(P2P::test), 0,obj);

    return NS_OK;//obj->Status(str);

And here is a thread function

When I call function before calling thread It works But As soon as I write something like obj->Status(temp); Firefox crashes on function this call

class P2P{
    static char RecvBuf[1024];
public:
    static void test(Callback* obj){
    //  char* temp="Hellllllooo";
    //  obj->Status(temp);
  return;
}

};

Upvotes: 0

Views: 402

Answers (1)

sdwilsh
sdwilsh

Reputation: 4682

When your code that begins the thread falls out of scope, the nsCOMPtr will release the object, putting its refcount to zero. At this point, the object will be deleted. You'll need call NS_ADDREF before you fall out of scope (and be sure to call NS_RELEASE when you are done with it so you don't leak!).

Upvotes: 1

Related Questions