java - JavaCV native object deallocation -
what's wrong following javacv code? try populate cvseq further work, jvm almost reliably crashes exception_access_violation @ various places, @ [msvcr100.dll+0x3c19b] memcpy+0x20b
or [opencv_core243.dll+0x61793] cvseqpush+0x133
public static void main(string[] args) { cvmemstorage memstorage = cvmemstorage.create(); cvseq seq = cvcreateseq(0, loader.sizeof(cvseq.class), loader.sizeof(cvpoint.class), memstorage); (int j=0; j<1000000; j++) { cvpoint cvpoint = cvpoint(j, j+1); system.out.println(j); cvseqpush(seq, cvpoint); } }
in configuration fails after 50000 iterations, @ other count or not @ all. there apparently allocation/deallocation error. , reproduces when not running in debug mode.
if explicitly call system.gc()
after 20000 iterations, fails inside cvseqpush
right after gc (or 1-2 iterations later, because pointer deallocated space happens point correct address). or if set both xmx
, xms
parameters, fails sooner or later. in use automagically deallocated.
the problem is, garbage collector recognizes memstorage
unused after last referenced in code, not @ end of block. causes native object deallocation after first gc happened in loop. that's why problem reproduced when not using debugger.
solution add reference memstorage
after loop, prevent gc freeing it. best call memstorage.release()
proactively free native memory without waiting gc , preventing premature deallocation while still needed.
see discussion javacv , javacpp author samuel audet few more native allocation questions: https://groups.google.com/forum/?fromgroups=#!topic/javacv/ffqskfxnt0o
Comments
Post a Comment