Thanks a lot

I have fixed this bug (Long time without coding in C). But still the error. Then I removed all malloc from the code. Still crashing

Examples of stack traces:

Stack: [0x00007fff8c015000,0x00007fff8c215000), sp=0x00007fff8c212b58, free space=2038k Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)

[error occurred during error reporting, step 120, id 0xb]

Stack: [0x0000000041e03000,0x0000000041f04000), sp=0x0000000041f02970, free space=1022k Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
V  [libjvm.so+0x318849]
V  [libjvm.so+0x62b60c]
V  [libjvm.so+0x62dc30]
V  [libjvm.so+0x5fa803]
V  [libjvm.so+0x5cf95d]
V  [libjvm.so+0x66a6fd]
V  [libjvm.so+0x66a0fa]
V  [libjvm.so+0x56b5e5]

Stack: [0x0000000041dbd000,0x0000000041ebe000), sp=0x0000000041ebc4b0, free space=1021k Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
V  [libjvm.so+0x2e6215]
V  [libjvm.so+0x3856d3]
V  [libjvm.so+0x317b47]
V  [libjvm.so+0x317db0]
V  [libjvm.so+0x62b466]
V  [libjvm.so+0x62d97b]
V  [libjvm.so+0x320ce2]
V  [libjvm.so+0x2e4860]
V  [libjvm.so+0x320782]
V  [libjvm.so+0x2adefd]
V  [libjvm.so+0x3209be]
V  [libjvm.so+0x66ae56]
V  [libjvm.so+0x66abda]
V  [libjvm.so+0x66a2f6]
V  [libjvm.so+0x66a56b]
V  [libjvm.so+0x66a0fa]
V  [libjvm.so+0x56b5e5]

VM_Operation (0x00000000411747b0): generation collection for allocation, mode: safepoint, requested by thread 0x00007fb2705448a0

Please help.

And here is the whole code:
#include <jni.h>

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/shm.h>

#include <sys/mman.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <unistd.h>

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

#include <wand/magick_wand.h>


#define False 0

#define True 1

#define DEFAULT_QUALITY 75

#define DECREASE_QUALITY_LEVEL 5


#define ThrowAPIException(wand) \

{ \

description=MagickGetException(wand,&severity); \

(void) fprintf(stderr,"%s %s %d %.1024s\n",GetMagickModule(),description); \

DestroyMagickWand(wand); \

free(description); \

}


int reduce_quality_to(const char * srcFile, const char * dstFile, int quality){

char *description;

ExceptionType severity;

MagickWand *magick_wand;

unsigned int status;


magick_wand = NewMagickWand();


status = MagickReadImage(magick_wand, srcFile);

if (status == False) {

ThrowAPIException(magick_wand);

return False;

}

MagickSetCompressionQuality(magick_wand, quality);

status=MagickWriteImages(magick_wand, dstFile,True);

if (status == False){

ThrowAPIException(magick_wand);

return False;

}

DestroyMagickWand(magick_wand);

return True;

}


JNIEXPORT jboolean JNICALL Java_com_vehicle_netapp_backend_objects_photo_PhotoOperations_reduceQualityTo

(JNIEnv * env, jobject jobj, jstring src, jstring dst, jint quality){

jboolean iscopy;

const char *srcFile = (*env)->GetStringUTFChars(env, src, &iscopy);

const char *dstFile = (*env)->GetStringUTFChars(env, dst, &iscopy);

int ret = reduce_quality_to(srcFile, dstFile, quality);

(*env)->ReleaseStringUTFChars(env, src, srcFile);

(*env)->ReleaseStringUTFChars(env, dst, dstFile);

return ret;

}


int copy_file(const char * src, const char * dst){

int inF, ouF;

char buff[1024*1024];

int bytes;


if (strcmp(src, dst)==0) return False;


if((inF = open(src, O_RDONLY)) == -1) {

perror("open");

return False;

}


if((ouF = open(dst, O_WRONLY | O_CREAT)) == -1) {

perror("open");

return False;

}


while((bytes = read(inF, buff, sizeof(buff))) > 0)

write(ouF, buff, bytes);


close(inF);

close(ouF);

return True;

}


int reduce_quality(const char * srcFile, const char * dstFile, int maxSize){

struct stat info;

stat(srcFile, &info);


if (info.st_size <= maxSize){//it is already small enough!! so just copy it

return copy_file(srcFile, dstFile);

}


int quality = DEFAULT_QUALITY;

long targetSize;

long lastTargetSize = 0;

int ret = False;

do {

ret = reduce_quality_to(srcFile, dstFile, quality);

if (ret == False) break;


stat(dstFile, &info);

targetSize = info.st_size;


if (targetSize == lastTargetSize) {

ret = reduce_quality_to(srcFile, dstFile, quality + DECREASE_QUALITY_LEVEL);

break;

}

lastTargetSize = targetSize;

quality -= DECREASE_QUALITY_LEVEL;

} while (quality > 0 && targetSize > maxSize);

if (quality == 0)

fprintf(stderr,"Couldn't reach desired size %d for %s\n", maxSize, dstFile);


return ret;

}


JNIEXPORT jboolean JNICALL Java_com_vehicle_netapp_backend_objects_photo_PhotoOperations_reduceQuality

(JNIEnv * env, jobject jobj, jstring src, jstring dst, jint maxSize){

jboolean iscopy;

const char *srcFile = (*env)->GetStringUTFChars(env, src, &iscopy);

const char *dstFile = (*env)->GetStringUTFChars(env, dst, &iscopy);

int ret = reduce_quality(srcFile, dstFile, maxSize);

(*env)->ReleaseStringUTFChars(env, src, srcFile);

(*env)->ReleaseStringUTFChars(env, dst, dstFile);

return ret;

}


int resize(const char * srcFile, const char * dstFile, int width, int height, int quality){

char *description;

ExceptionType severity;

MagickWand *magick_wand;

unsigned int status;


magick_wand = NewMagickWand();


status = MagickReadImage(magick_wand, srcFile);

if (status == False) {

ThrowAPIException(magick_wand);

return False;

}

MagickResizeImage(magick_wand,width,height,LanczosFilter,1.0);

MagickSetCompressionQuality(magick_wand, quality);

status=MagickWriteImages(magick_wand, dstFile,True);

if (status == False){

ThrowAPIException(magick_wand);

return False;

}

DestroyMagickWand(magick_wand);

return True;

}


 

int resize_with_limits(const char * srcFile, const char * dstFile, int maxDim, int maxSize){

char *description;

ExceptionType severity;

MagickWand *magick_wand;

unsigned int status;


magick_wand = NewMagickWand();


status = MagickReadImage(magick_wand, srcFile);

if (status == False) {

ThrowAPIException(magick_wand);

return False;

}


int originW = MagickGetImageWidth(magick_wand);

int originH = MagickGetImageHeight(magick_wand);


int ret;

/*

* the original image is smaller than limits so just copy it

*/

if(originH<=maxDim && originW<=maxDim){

if (maxSize > 0) {

ret = reduce_quality(srcFile, dstFile, maxSize);

} else {

ret = copy_file(srcFile, dstFile);

}

}

else {

double scale;

if (originW > originH)

scale = (double)maxDim/(double)originW;

else

scale = (double)maxDim/(double)originH;


int scaledW = (int)(scale * originW);

int scaledH = (int)(scale * originH);


if(maxSize>0){

int quality = DEFAULT_QUALITY;

long targetSize;

long lastTargetSize = 0;

do{

resize(srcFile, dstFile, scaledW, scaledH, quality);

struct stat info;

stat(srcFile, &info);

targetSize = info.st_size;

if(targetSize == lastTargetSize){

resize(srcFile, dstFile, scaledW, scaledH, quality + DECREASE_QUALITY_LEVEL);

break;

}

lastTargetSize = targetSize;

quality-= DECREASE_QUALITY_LEVEL;

}while(quality > 0 && targetSize > maxSize);

if(quality==0)

fprintf(stderr,"Couldn't reach desired size %d for %s\n", maxSize, dstFile);

}

else

resize(srcFile, dstFile, scaledW, scaledH, DEFAULT_QUALITY);

}

DestroyMagickWand(magick_wand);

return True;

}


 

JNIEXPORT jboolean JNICALL Java_com_vehicle_netapp_backend_objects_photo_PhotoOperations_resize__Ljava_lang_String_2Ljava_lang_String_2II

(JNIEnv * env, jobject jobj, jstring src, jstring dst, jint maxDim, jint maxSize){

jboolean iscopy;

const char *srcFile = (*env)->GetStringUTFChars(env, src, &iscopy);

const char *dstFile = (*env)->GetStringUTFChars(env, dst, &iscopy);

int ret = resize_with_limits(srcFile, dstFile, maxDim, maxSize);

(*env)->ReleaseStringUTFChars(env, src, srcFile);

(*env)->ReleaseStringUTFChars(env, dst, dstFile);

return ret;

}


JNIEXPORT jboolean JNICALL Java_com_vehicle_netapp_backend_objects_photo_PhotoOperations_resize__Ljava_lang_String_2Ljava_lang_String_2III

(JNIEnv * env, jobject jobj, jstring src, jstring dst, jint width, jint height, jint quality){

jboolean iscopy;

const char *srcFile = (*env)->GetStringUTFChars(env, src, &iscopy);

const char *dstFile = (*env)->GetStringUTFChars(env, dst, &iscopy);

int ret = resize(srcFile, dstFile, width, height, quality);

(*env)->ReleaseStringUTFChars(env, src, srcFile);

(*env)->ReleaseStringUTFChars(env, dst, dstFile);

return ret;

}


int crop(const char * srcFile, const char * dstFile, int widthRatio, int heightRatio){

char *description;

ExceptionType severity;

MagickWand *magick_wand;

unsigned int status;


magick_wand = NewMagickWand();


status = MagickReadImage(magick_wand, srcFile);

if (status == False) {

ThrowAPIException(magick_wand);

return False;

}

unsigned long h = MagickGetImageHeight(magick_wand);

unsigned long w = MagickGetImageWidth(magick_wand);


MagickCropImage(magick_wand, w * (widthRatio/100.0), h * (heightRatio/100.0), 0, 0);

status=MagickWriteImages(magick_wand, dstFile,True);

if (status == False){

ThrowAPIException(magick_wand);

return False;

}

DestroyMagickWand(magick_wand);

return True;

}


JNIEXPORT jboolean JNICALL Java_com_vehicle_netapp_backend_objects_photo_PhotoOperations_crop

(JNIEnv * env, jobject jobj, jstring src, jstring dst, jint widthRatio, jint heightRatio){

jboolean iscopy;

const char *srcFile = (*env)->GetStringUTFChars(env, src, &iscopy);

const char *dstFile = (*env)->GetStringUTFChars(env, dst, &iscopy);

int ret = crop(srcFile, dstFile, widthRatio, heightRatio);

(*env)->ReleaseStringUTFChars(env, src, srcFile);

(*env)->ReleaseStringUTFChars(env, dst, dstFile);

return ret;

}


int compose(const char * composeImgFile, const char * imageFile, int position, int width, int height, int shiftDirection, int shiftAmount){

char *description;

ExceptionType severity;

MagickWand *magick_wand, *composite_wand;

unsigned int status;


magick_wand = NewMagickWand();

composite_wand = NewMagickWand();


status = MagickReadImage(magick_wand, imageFile);

if (status == False) {

ThrowAPIException(magick_wand);

return False;

}


status = MagickReadImage(composite_wand, composeImgFile);

if (status == False) {

ThrowAPIException(composite_wand);

DestroyMagickWand(magick_wand);

return False;

}

int w,h;

if (width >0 && height >0){

w=width;

h=height;

MagickResizeImage(composite_wand,width,height,LanczosFilter,1.0);

}

else{

h = MagickGetImageHeight(composite_wand);

w = MagickGetImageWidth(composite_wand);

}

int imgH = MagickGetImageHeight(magick_wand);

int imgW = MagickGetImageWidth(magick_wand);

int x=0,y=0;

switch(position){

case 1://North west

y=0;

x=0;

break;

case 2://North east

y=0;

x=imgW-w;

break;

case 3://South west

y=imgH -h;

x=0;

break;

case 4://South east

y=imgH-h;

x=imgW-w;

break;

case 5://center

y=(imgH-h)/2;

x=(imgW-w)/2;

break;

}

switch(shiftDirection){

case 1://North

y-=shiftAmount;

break;

case 2://East

x+=shiftAmount;

break;

case 3://South

y+=shiftAmount;

break;

case 4://West

x-=shiftAmount;

break;

}

MagickCompositeImage(magick_wand, composite_wand, OverCompositeOp, x, y);

status=MagickWriteImages(magick_wand, imageFile,True);

if (status == False){

ThrowAPIException(magick_wand);

DestroyMagickWand(composite_wand);

return False;

}

DestroyMagickWand(magick_wand);

DestroyMagickWand(composite_wand);

return True;

}


JNIEXPORT jboolean JNICALL Java_com_vehicle_netapp_backend_objects_photo_PhotoOperations_compose

(JNIEnv * env, jobject jobj, jstring composeImg, jstring image, jint position, jint width, jint height, jint shiftDirection, jint shiftAmount){

jboolean iscopy;

const char *composeImgFile = (*env)->GetStringUTFChars(env, composeImg, &iscopy);

const char *imageFile = (*env)->GetStringUTFChars(env, image, &iscopy);

int ret = compose(composeImgFile, imageFile, position, width, height, shiftDirection, shiftAmount);

(*env)->ReleaseStringUTFChars(env, composeImg, composeImgFile);

(*env)->ReleaseStringUTFChars(env, image, imageFile);

return ret;

}



Mohamedin
----- Original Message ----- From: "Konstantin Kolinko" <knst.koli...@gmail.com>
To: "Tomcat Users List" <users@tomcat.apache.org>
Sent: Thursday, October 08, 2009 10:37 AM
Subject: Re: JNI problem


2009/10/8 Mohamedin <mohame...@easy-dialog.info>:
Here is one function that always crash. The portion of the code that couse
the crash in my opinion is the if which has malloc. I noticed it crash
whenever it is called with the same srcFile and dstFile

int reduce_quality(const char * srcFile, const char * dstFile, int maxSize){

...
tmpFile = (char *) malloc(sizeof(srcFile)+1);

You meant string length there?   sizeof(pointer) is - what is the size
of pointers on your OS?  8 bytes?

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org


__________ Information from ESET NOD32 Antivirus, version of virus signature database 4488 (20091007) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com





__________ Information from ESET NOD32 Antivirus, version of virus signature 
database 4488 (20091007) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com




---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to