简体   繁体   中英

I get java.lang.StackOverflowError: stack size 8MB when i draw my canvas

I'm trying to draw some points on my view using a canvas. But as soon i try to draw my canvas on my view with

 view.draw(canvas);

I get the error java.lang.StackOverflowError: stack size 8MB and then my app crash immediately. Does someone has an idea how i can reduce the size of my canvas or fix this error ?

Here is my current code :

onDrawListener = new OnDrawListener() {
                @Override
                public void onLayerDrawn(Canvas canvas, float pageWidth, float pageHeight, int displayedPage) {
                    try {
                        Log.d("PdfViewer", "Drawing view");
                        Paint paint = new Paint();
                        paint.setColor(Color.RED);
                        canvas.drawPoint(canvas.getWidth() /2, canvas.getHeight() /2, paint);
                        view.draw(canvas); //here i get the error
                    } catch (Exception e) {
                        Log.d("PdfViewer", e.toString());
                    }


                }
            };

Thank you

The line you mentioned calls view.draw(canvas); This calls your onDrawListener , which calls view.draw(canvas) , which calls your onDrawListener , which calls view.draw(canvas) , and so on...

Every time a function calls another function, it passes data to the function it is calling through a "stack frame". This takes space in memory, and each stack frame gets freed when the function it is associated with returns. Since your onDrawListener calls itself, it will never return, and that memory will keep getting used, until there is none left.

Remove the line you highlighted, and, assuming no other issues are present, it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM