Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak on Sockets in iOS #3726

Open
shai-almog opened this issue Aug 19, 2023 · 1 comment
Open

Memory leak on Sockets in iOS #3726

shai-almog opened this issue Aug 19, 2023 · 1 comment
Assignees

Comments

@shai-almog
Copy link
Collaborator

This leak is in the input/output stream of the native OS allocated here: https://github.com/codenameone/CodenameOne/blob/master/Ports/iOSPort/nativeSources/SocketImpl.m#L25

They are released on disconnect here so it seems this isn't invoked probably because this code isn't invoked due to isSocketConnected returning false.

I'm guessing that this is because socket doesn't have a finalizer that invokes dispose for us. That would explain the iOS specific behavior.

        terminalStatusThread.run(() -> 
        {
            try
            {
                InetAddress localAddress = InetAddress.getLocalHost();
                String      ip           = localAddress.getHostAddress();
                String      segment      = ip.substring(0, ip.lastIndexOf(".") + 1);

                for(int i=1;i<255;i++)
                {
                    String current = segment + i;

                    if (!ip.equals(current) && !excludeIps.contains(ip))
                    {
                        Socket socket = new Socket(10);

                        synchronized(socket)
                        {
                            com.codename1.io.Socket.connect(current, DEFAULT_REST_API_PORT, socket);
                            socket.wait();
                        }

                        if (socket.isConnected())
                        {
                            socket.close();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                //Log.e(e);
            }
        });


    class Socket extends SocketConnection {
        private InputStream is;
        
        public Socket(int connectTimeout)
        {
            super(connectTimeout);
        }
        
        @Override
        public synchronized void connectionError(int errorCode, String message)
        {
            notify();
        }

        @Override
        public synchronized void connectionEstablished(InputStream is, OutputStream os)
        {
            this.is = is;
            notify();
        }
        
        public synchronized void close()
        {
            if (isConnected())
            {
                try
                {
                    is.close();
                }
                catch(IOException e)
                {
                    com.codename1.io.Log.e(e);
                }
            }
        }
    }
@shai-almog shai-almog self-assigned this Aug 19, 2023
shai-almog added a commit that referenced this issue Aug 31, 2023
shai-almog added a commit that referenced this issue Oct 28, 2023
Ideally should fix both #3726 and the regression in #3753
@ddyer0
Copy link
Contributor

ddyer0 commented Oct 28, 2023

Let me know when this makes it to the public builds and I’ll retest.

I’m still concerned that this bug apparently killed the gc with no immediate effect, leaving a crippled app to die at some later time.

shai-almog added a commit that referenced this issue Nov 4, 2023
This occurred due to the fix for #3726 which allowed the GC to collect the output/input streams before the socket itself was collected
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants