views:

755

answers:

1

Hi,

I'm trying to learn how to write a Firefox plugin. I downloaded the npruntime example from Mozilla compiled it and ran it. Getting properties from the plugin worked well but when I tried to call a method, Firefox freezed. I thought maybe something is wrong with the example, so I wrote my own basic scriptable plugin that has one property and one method which returns a string. The property worked well, but calling the method caused Firefox to freeze, again.

Am I missing something? I tried debugging the plugin and everything seems fine. All the right functions are called and the value is returned properly.If I try to stop the process while Firefox hangs, I get stopped at a Windows DLL, not in my code and not in Firefox code.

If anyone can point me to the right direction...

Thanks.

+5  A: 

I hope that you've got it solved. If this is not the case, I've just discovered that the example (I assume that was the damned "npruntime sample") was flawed.

In returning a string, the example used the function strdup to allocate a string passed with a NP_something method. Fact is that NPAPI takes care of the allocated string from that point on and, when tries to destroy it, it cannot since strdup uses malloc and not NPN_MemAlloc.

The solution is to NEVER use malloc or new for objects that we pass to NPAPI functions. In the npruntime sample the error is at line 452:

STRINGZ_TO_NPVARIANT(strdup("foo return val"), *result);

and line 466:

STRINGZ_TO_NPVARIANT(strdup("default method return val"), *result);

I've corrected it with this code:

char* src = "foo return val";
char* out = (char *)NPN_MemAlloc(strlen(src) + 1);
strcpy(out, src);
STRINGZ_TO_NPVARIANT(out, *result);

and it worked. But one would think that such a flaw in a sample should be corrected by mozilla SDK maintainers.

cerbert
Nickolay
Hey, thanks! One of our developers has filed a bug and put up a patch with your fix in it. If you'd like to get named credit, drop in the bug and state your name.https://bugzilla.mozilla.org/show_bug.cgi?id=530138
sdwilsh