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

App crashes when upload a file which size is bigger than 5M #41

Closed
Deerdev opened this issue Dec 1, 2016 · 18 comments
Closed

App crashes when upload a file which size is bigger than 5M #41

Deerdev opened this issue Dec 1, 2016 · 18 comments

Comments

@Deerdev
Copy link

Deerdev commented Dec 1, 2016

No description provided.

@TimOliver
Copy link
Owner

Um. Are there any error messages that appear when it crashes?

@Deerdev
Copy link
Author

Deerdev commented Dec 7, 2016

img_1888

throw "EXC_BAD_ACCESS" when go into the loop first time;

@xorander00
Copy link

I'm having the same issue. Haven't had the chance to fully figure it out, but it seems that it might be an issue within libdsm. Everything passed into smb_fwrite seems kosher. I confirmed that the buffer is valid and that the buffer size was properly specified. I'm going to try to take a deeper look at it next week, but if anyone else finds anything in the meantime, I'd like to hear about it here :)

@TimOliver
Copy link
Owner

TimOliver commented Jan 15, 2017

Hmm, if everything on our end seems kosher, it might be a libdsm level issue then...

@xorander00
Copy link

Possibly, but I wouldn't assume that just yet. Ultimately, it's libdsm being unable to access the data buffer. Currently, I suspect that it's either being free()'ed/released too early (whether it's in TOSMBClient or libdsm) or it's some kind of access issue (threading?). I'm not too familiar with Objective-C, but I have plenty of experience with C and LLVM, so I'll dig in later this week.

In my case, I get that error whenever the data being written out is greater than the write buffer size (64k - header size). It happens way below 5MB for me. I tried playing around with the smb_fopen flags, but to no avail. I also tried reducing the buffer size to see if the header size might have been changed somehow, but that didn't work either. It does create create the file though, it just ends up being 0 bytes.

Oh, and also, I found a small bug a week ago, but haven't had the chance to submit a PR yet (don't do this very often, sorry). I can do one later this week, but it's literally a one-liner if you want to make the quick change yourself. It's in TOSMBSessionUploadTask.m @ line 222. Right after the free(buffer) call, I added smb_fclose(self.smbSession, fileID). Otherwise it holds the file lock and the file can't be deleted until libdsm releases the lock. Found it while debugging this issue, ended up with a bunch of 0-byte files that I couldn't delete lol.

@kamsont
Copy link

kamsont commented Jan 26, 2017

Any news on this issue? I'm having trouble uploading files >150KB. Other people claim that they were able to upload >1GB using libdsm (post from video labs)

@xorander00
Copy link

I'm actually investigating it further later today & tomorrow. I'm not yet 100% convinced that it's a libdsm issue. I still suspect that it has to do with the Objective-C malloc'ed memory buffer. Alternatively, it could be a protocol-specific issue as well. I actually spent some time a few days ago reading up on the SMB spec and will now be going through the libdsm code a bit more along with the code in this repo.

If I find something, I'll post here in the coming few days.

@xnz23
Copy link

xnz23 commented Feb 6, 2017

Same issue here!!!

@peeeble
Copy link

peeeble commented Feb 9, 2017

Hey this is very interesting - got the same problem... download is no problem at any size but upload is limited to small files. @xorander00 did you already figured out if the problem is within libdsm? Sadly i`m not experienced enough in C.

@DemianSteelstone
Copy link

I'm sure that libdsm works fine. I have no problems with it in my fork.
also you can try to decrease chunk size

@kamsont
Copy link

kamsont commented Feb 10, 2017

I tried to upload bigger files with your forked version - no success, same exception thrown. Could you specify what you mean with chunk size? The buffer size?

@DemianSteelstone
Copy link

Yep, I mean buffer size.
I can successfully upload files more then 30 mb
Which branch you're try?

@kamsont
Copy link

kamsont commented Feb 10, 2017

You are right. I have been using the master branch for a test. In the developer branch the upload is passing without exception. Is it right that all you had to do is writing the file to your device first before uploading to avoid the malloc problem?

@DemianSteelstone
Copy link

I have changed write with data to stream writing to avoid problems with the lack of memory and reduce buffer size to 64000 bytes. Don't know why, but it crashes with larger size.

@narendra-ct
Copy link

@sweetfish I'm not sure why this happening, i faced same issue while using this lib, for the solution i tried by changing buffer size

#define BUFFER_UPLOAD_SIZE 60000

and refer following code you may get some idea.

_fp = fopen([filePath UTF8String],"r");
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];
self.countOfBytesExpectedToReceive = (ssize_t)[fileHandle seekToEndOfFile];

BOOL success = TRUE;
int64_t _length = 0;
if (_fp != NULL)
{
    while (_read_length != 0)
    {
        //Write the bytes to the network device
        @autoreleasepool
        {
            _read_length = fread(_buffer, 1, BUFFER_UPLOAD_SIZE, _fp);
            if (_read_length != 0 && _read_length > 0)
            {
                _length = smb_fwrite(_uploadSession, uploadFileID, _buffer, _read_length);
                if (_length > 0)
                {
                    self.countOfBytesReceived += _read_length;
                    progress(_length,self.countOfBytesReceived,self.countOfBytesExpectedToReceive);
                }
                else
                {
                    _read_length = 0;
                    success = FALSE;
                }
            }
        }
        
        if (weakOperation.isCancelled)
        {
            _read_length = 0;
            success = FALSE;
            //                break;
        }
    }
    
}
else
{
    success = FALSE;
}

if (_buffer != NULL) {
    free(_buffer);
}

Thanks,
Narendra.

@Deerdev
Copy link
Author

Deerdev commented Feb 15, 2017

@narendra-ct Thanks,
I changed buffer size, it's ok now.

Here is my code, may help to others:
wx20170215-161913 2x

@Deerdev Deerdev changed the title App crashes when upload a file whose size is bigger than 5M App crashes when upload a file which size is bigger than 5M Feb 15, 2017
@Deerdev Deerdev closed this as completed Feb 15, 2017
@TimOliver
Copy link
Owner

Oh awesome! Nicely done! Can you please file a PR with those updates? :)

@Deerdev
Copy link
Author

Deerdev commented Feb 16, 2017

Ok, let me do more test.

@Deerdev Deerdev mentioned this issue Mar 21, 2017
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

8 participants