ArrayList->GetAt(...) fails

Any new platform may create a countless amount of troubles on the way of a developer - either due to the bugs on the platform itself or due to the developer's limited know-how. Either way, when you find a solution to a particular problem in bada development, this is the place to offer it to fellow bada developers!

ArrayList->GetAt(...) fails

Postby wit » Tue Mar 09, 2010 12:02 pm

You may want to use the simple-looking bada collection types (Osp::Base::Collection) to manage sets of objects. Although they do look good, there are a few things you should be aware of!

Let me give you an example, you have a simple class. All you want it to do is to Add Strings returned by some method to an ArrayList and read a String at some position afterwards... here's a Dummy:

Syntax: [ Download ] [ Hide ]
Using cpp Syntax Highlighting
class Dummy
{
private:
ArrayList * list;

public:
void Construct()
{
    int i = 0;
    String str;
    list = new ArrayList();
    list->Construct(20);

    for(i=0; i<10;i++)
    {
        str = SomeOtherMethodReturningAString(i);
        list->Add(str);    
     }
}

String GetStringAt(int i)
{
    return *(static_cast<String *>(list->GetAt(i)));
}
};
 
Parsed in 0.008 seconds, using GeSHi 1.0.8.4


So you think GetStringAt(int i) will work without problem, right??? WRONG!

You see, ArrayList::Add(Object &obj) stores references of objects in the array. Unfortunately, once Construct() is left all what was str is deallocated!! So when you try to read data from those memory spaces afterwards in GetStringAt(), the simulator crashes!

You have to keep those String objects in memory, which means, creating them in heap through pointers/new operator:

Syntax: [ Download ] [ Hide ]
Using cpp Syntax Highlighting
class Dummy
{
private:
ArrayList * list;

public:
void Construct()
{
    int i = 0;
    String * str;
    list = new ArrayList();
    list->Construct(20);

    for(i=0; i<10;i++)
    {
        str = new String(SomeOtherMethodReturningAString(i));
        list->Add(*str);    
     }
}

String GetStringAt(int i)
{
    return *(static_cast<String *>(list->GetAt(i)));
}
};
 
Parsed in 0.008 seconds, using GeSHi 1.0.8.4


See the difference in str? Now you have created a bunch of String objects that exist all by themselves in memory. What if you wanted to delete them? You have no reference anymore! Isn't it a memory leak?

The answer is: No. ArrayList list still has references to it. If you wanted to delete an object you can use

ArrayList::RemoveAt(int index, bool deallocate=false)

See the deallocate parameter? If you set it to true, not will just the reference be removed from the list, but the memory will be wiped clean, too!

You are welcome! :mrgreen:
Wit. Pirate. 해적

:pirate-cool:
BadaDev

Image
User avatar
wit
Founder
 
Posts: 930
Joined: Wed Nov 25, 2009 8:17 am
Location: Germany

Re: ArrayList->GetAt(...) fails

Postby Irony » Fri Aug 27, 2010 6:32 pm

THANK YOU!!!! you are my hero, I spent so many hours trying to figure out what was happening yesterday.
-Irony
:geek: :pirate-bird:
User avatar
Irony

 
Posts: 41
Joined: Thu Jun 17, 2010 6:27 pm
Location: California, USA


Return to Solutions

Who is online

Users browsing this forum: No registered users and 1 guest