Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...with companys cutting back on training, lack of true support by makers of software, the forums are a great tool in your cyber-toolbox...."

Geography

Where in the world do Tek-Tips members come from?

BorlandBuilderC++ Allocation Failure

MrEARTHSHAcKER (Programmer)
13 Jul 12 15:40
Hi,

My problem is allocation failure.
Namely, I need to dynamically create 39 images, load pictures for them, add OnClick action and arrange them in Form.

Everything works perfectly through this code:

CODE --> C++

class MyImage{           //Class groups Index and *image, I need Index because of 
                         //ImageClickEvent function,  but it ain't matter now
        public:
        TImage *image;
        int Index;
        MyImage(){
        image=new TImage(TicketTab); }      //When object is created, it allocates *image
        ~MyImage(){  delete image;}
        void __fastcall ImageClickEvent(TObject* Sender);
};


void __fastcall MyImage::ImageClickEvent(TObject* Sender){            //This function is being passed to default function
        image->Picture->LoadFromFile(
        Path->TexturesPath()+"\\TicketTab\\Numbers\\x"+IntToStr(Index+1)+".bmp");

	//some code
        }

}
#define MAX 39  //Number of pictures
MyImage *MyImages[MAX]; 

//---------------------------------------------------------------------------

void __fastcall TTicketTab::FormCreate(TObject *Sender)
{

//--------------------------------



int _Top=144;      //Starting coordinates
int _Left=184;



for(int i=0;i<MAX;++i);          //Allocate memory for 39 objects
MyImages[i]=new MyImage;
  

for(int i=0;i<MAX;++i){          //Everything in this loop is just setting propertis and position of images

        MyImages[i]->Index=i;
        MyImages[i]->image->Parent=this;
        MyImages[i]->image->Picture->LoadFromFile(
        Path->TexturesPath()+"\\TicketTab\\Numbers\\"+IntToStr(i+1)+".bmp");

        MyImages[i]->image->Width=25;
        MyImages[i]->image->Height=25;
        MyImages[i]->image->Stretch=true;
        MyImages[i]->image->Visible=true;
        MyImages[i]->image->OnClick=MyImages[i]->ImageClickEvent;

        MyImages[i]->image->Top=_Top;
        MyImages[i]->image->Left=_Left;
        if((_Left==248 && _Top==272) ||(_Left==352 && _Top==272)){
                _Left+=40;
                _Top=144;

        }

        else if(_Left==248||_Left==352||_Left==456){
                _Left-=64;
                _Top+=32;
        }
        else if(_Left==456 && _Top==208)
                break;
        else _Left+=32;

} 


More details about this: from Main form, I dynamically call form where I should do all of this stuff. It works perfectly when I do it first time after launching app.
But when I close that form (TicketTab), and start it again from Main form, I get error:


And I think it is bad allocation because of three things:
1.Doesn't happen always, sometimes after 2nd or 3rd launch;
2.Considering Borland doesn't terminate app after this, I can "continue" running it, and it opens TicketTab, but no images were created.
3.After Borland "pauses" app, this line gets highlighted:

CODE --> C++

MyImage(){
        image=new TImage(TicketTab); } 

Does anyone have the idea of decreasing possibility of allocation failure?


Thanks!
ArkM (IS/IT--Management)
14 Jul 12 0:26

CODE

for(int i=0;i<MAX;++i);          //Allocate memory for 39 objects
MyImages[i]=new MyImage; 
Look at semicolon after loop header.
You DO NOT create objects in this loop: it has an empty body.
It's a very strange code: there is (modern) C++ error (undefined i) in the 2nd statement.
MrEARTHSHAcKER (Programmer)
14 Jul 12 3:34
Hi ArkM,

I don't know from where did that semicolon appear. Probably during copy-paste and deleting not necessary stuff from this piece of code.
It'd show me that variable 'i' was not defined, before compiling, just as you said.

However, do you have any explanation about error that occurs?

Thanks, and sorry about code, my fault.

MrEARTHSHAcKER (Programmer)
14 Jul 12 3:44
*Update*

I removed constructor

CODE --> C++

MyImage(){
        image=new TImage(TicketTab); } 

So it doesn't allocate memory when object of MyImage is created, but I have put allocation process into for loop:


CODE --> c++

for(int i=0;i<MAX;++i){

        MyImages[i]->image=new TImage(TicketTab); //added line
        MyImages[i]->Index=i;
        MyImages[i]->image->Parent=this;
        //rest of code in for loop 

After that, it seems that app shows error less frequently - it appears after 3rd opening of TicketTab.

Anyone experienced with this?
ArkM (IS/IT--Management)
14 Jul 12 6:48
It seems the error is outside these snippets context.
The second code variant is exactly the same as the first one. It's useless code refactoring.
Insufficient info: we can't see MyImages array and Path pointer definitions, we know nothing about TiketTab scope etc...
MrEARTHSHAcKER (Programmer)
14 Jul 12 7:44
Hmm.. let me provide more informations.

TicketTab is "available form" which is being called using this code:

CODE --> C++

TicketTab=new TTicketTab(Applicaton);
this->Hide();
TicketTab->ShowModal(); 


"this" presents "Auto-create form" MainTab, and its button calls TicketTab using code from above.

If we'd isolate these two forms and remove absolutely all additional headers (headers of mine, necessary for app ). We'd keep same behaviour.

Here is my test-project which explains this.
MrEARTHSHAcKER (Programmer)
14 Jul 12 7:55

Quote (ArkM)

Insufficient info: we can't see MyImages array and Path pointer definitions, we know nothing about TiketTab scope etc...

In project I provided in last post, you don't need Path pointer definition, just change default location of images (which are in the same folder as project, but however, change the path).

I just asked if someone has had this experience, since I see it is an awkward error, so ArkM if you don't see any solution on first check, don't bother with it. thumbsup2
MrEARTHSHAcKER (Programmer)
15 Jul 12 11:52
I found solution for problem!

Just replace "TicketTab" with "this". Here:


CODE --> C++

MyImage(){
        image=new TImage(TicketTab); } 


I hope someone finds this helpful!!

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close