Greetings,

I came across a memory allocation within the EventHub.cpp that I think
can potentially leak and even cause a runtime error. The code is bit
different in Gingerbread and Froyo but both have the same problem.
Here is the snippet from the Gingerbread:

int EventHub::openDevice(const char *deviceName) {

...

x1)  new_mFDs = (pollfd*)realloc(mFDs, sizeof(mFDs[0]) * (mFDCount +
1));
x2)    new_devices = (device_t**)realloc(mDevices, sizeof(mDevices[0])
* (mFDCount + 1));
x3)    if (new_mFDs == NULL || new_devices == NULL) {
x4)        LOGE("out of memory");
x6)        return -1;
x7)    }
x8)    mFDs = new_mFDs;
x9)    mDevices = new_devices;

...
}

new_mFDs is a local pointer. Consider this scenario: memory is
allocated successfully at line x1, but the allocation at line x2
fails. Then, the function returns without talking
care of the newly allocated new_mFDs (the new pointer is saved at line
x8.) However, this is more serious that a memory leak; this is also a
dangling reference problem. Consider the case that (no matter how
unlikely) the memory manager cannot extend the memory block and
therefore allocates a new bigger block somewhere in the memory
and copies the old block. ( Then line x2 fails and the function
returns.) what happens is that the newly allocated block leaks and the
old pointer (mFDs) is a dangling reference
that we are holding on to and using everywhere, which most probably
would crash the process.

This was my observation and I hope I can get some feedback on this.

Thanks

--Hossein

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to