MAUtil/Vector.h BUG in MoSync 2.5

5 posts / 0 new
Last post
galilov
galilov's picture
Offline
Joined: 14 Jul 2011
Posts:
MAUtil/Vector.h BUG in MoSync 2.5

Hi! I found a bug in Vector class:

Line 177, Vector.h

/** \brief Adds several elements to the end of the Vector.
 *  \param ptr A pointer to the elements.
 *  \param num The number of elements.
 */
void add(const Type* ptr, int num) {
	int neededCapacity = mSize + num;
	if(mCapacity < neededCapacity) {
		do {
			mCapacity *= 2; // PAY ATTANTION HERE !!!!
		} while(mCapacity < neededCapacity);
		reserve(mCapacity);   // AND NOW WE HAVE A BUG - SEE BELOW
	}
	for(int i=0; i<num; i++) {
		mData[mSize++] = *(ptr++);
	}
}

After the class member "mCapacity" has been changed the method "reserve" is executed:

Line 289, Vector.h:

/** \brief Reserves space in the Vector.
 *  \param newCapacity The desired capacity of the Vector.
 *  \note If \a newCapacity is less than the current capacity of the Vector, nothing will happen.
 */
void reserve(int newCapacity) { // ATTANTION! newCapacity is equal to mCapacity (SEE ABOVE)
	MAUTIL_VECTOR_LOG("reserve 0x%08X %i", (int)this, newCapacity);
	if(newCapacity <= mCapacity) // TRUE
		return; // RETURN
	MAUTIL_VECTOR_LOG("reserve 2");
	Type* newData = new Type[newCapacity];
	MAUTIL_VECTOR_LOG("reserve 4");
	for(int i=0; i < mSize; i++) {
		newData[i] = mData[i];
	}
	MAUTIL_VECTOR_LOG("reserve 5");
	mCapacity = newCapacity;
	delete[] mData;
	MAUTIL_VECTOR_LOG("reserve 6");
	mData = newData;
	MAUTIL_VECTOR_LOG("reserve done");
}

As You can see "reserve" couldn't work properly when is is called from "void add(const Type* ptr, int num)" method.

But an other method "add" works properly because it doesn't attempt to change mCapacity member.

Line 164, Vector.h:

/** \brief Adds an element to the end of the Vector.
 *  \param val The element to be added.
 */
void add(const Type& val) {
	if(mSize >= mCapacity-1) {
		if(mCapacity != 0)
			reserve(mCapacity*2);
		else
			reserve(4);
	}
	mData[mSize++] = val;
}

Best regards,
Alexander Galilov

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Abi Waqas
abi's picture
Offline
Mobile Wizard
Joined: 3 May 2010
Posts:

Thanks Alexander for your input. There has been a lot of bug fixing for MoSync 2.6 Release. We will check if it has been fixed or not for MoSync 2.6. Good work!

gregorgullwi
gregorgullwi's picture
Offline
Mobile Conjurer
Joined: 13 Aug 2011
Posts:

I can confirm that this bug is still in 2.7 and caused me a lot of headache...

Abi Waqas
abi's picture
Offline
Mobile Wizard
Joined: 3 May 2010
Posts:

Hello Everybody,

pardon me for being so late, but there is a good news I have for you. This bug is now fixed and the fix should be available in the next nightly build.

Cheers,

Abi Waqas
abi's picture
Offline
Mobile Wizard
Joined: 3 May 2010
Posts:

As you would have known by now that MoSync Issue tracker has been migrated to Jira. You can track the progress of this issue here

http://jira.mosync.com/browse/MOSYNC-1414