English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. C/C++

 
 
Thread Tools
Old 04 June 2014, 03:07   #1
mritter0
Registered User

 
Join Date: Sep 2013
Location: Bettendorf, IA, USA
Age: 49
Posts: 204
struct Image

My program is having memory losses due to images I am putting in a listbrowser. I know what to do to fix it, I just don't know how.

- Open program, libs, set everything up, open window and gadgets
- Get a list of items, will vary in length every time
- Call a subroutine to create the listbrowser list
- That sub has a struct Image *ImgLabel that I use in a LABEL object with a PENMAP image.
- Return to main program body to do some stuff
- Go back to sub to fill in listbrowser with new data
- etc.

Code:
	ImgLabel=NewObject(LabelClass,NULL,
		IA_Font,					&xen8,
		LABEL_Image,				myImg,
		LABEL_Text,					" ",
		LABEL_Text,					"The Text",
		LABEL_DisposeImage,			TRUE,
	TAG_DONE);
	if (node=AllocListBrowserNode(1,
		LBNA_Generation,			1,
		LBNA_Flags,					LBFLG_HASCHILDREN|LBFLG_SHOWCHILDREN,
		LBNCA_Image,				ImgLabel,
//		LBNCA_CopyText,				TRUE,
//		LBNCA_Text,					"The Text",
		LBNA_UserData,				"The Text",
	TAG_END))
		AddTail(list,node);
All is fine, until I use the above code in a loop to fill in all items, which might be 5 to 100. The same ImgLabel struct/pointer is being used for every item.

@Thomas: Your ListBrowser2 program, for example. When you load a file, how would you add an image to every line so that it gets free'd correctly?

Code:
while(!Done)
{
    -create a new struct Image *ImgLabel    ????
    -do the above code snippet
    -free the struct    ????
    -repeat until Done
}
mritter0 is offline  
Old 04 June 2014, 05:11   #2
PeterK
Registered User
 
Join Date: Apr 2005
Location: Hangover
Posts: 2,860
I'm not a C programmer, but struct Image has nothing to do with any string pointers or text.

from structure.guide
Code:
Struct Image:
  $0014    20  sizeof(Image)
  $0000     0     2  LeftEdge
  $0002     2     2  TopEdge
  $0004     4     2  Width
  $0006     6     2  Height
  $0008     8     2  Depth
  $000a    10     4  ImageData
  $000e    14     1  PlanePick
  $000f    15     1  PlaneOnOff
  $0010    16     4  NextImage
ImgLabel is either a pointer to a memblock allocated for struct Image or ImgLabel is a pointer to an Object, not both at the same time, make a decision ...
or struct Image must be the first element in your Object,
but as I said, I don't no much about C ...

Last edited by PeterK; 04 June 2014 at 05:25.
PeterK is offline  
Old 04 June 2014, 08:45   #3
thomas
Registered User
thomas's Avatar
 
Join Date: Jan 2002
Location: Germany
Posts: 6,192
FreeListBrowserList frees all memory allocated by AllocListBroserNode for those nodes which are still in the list. If you remove nodes from the list you have to call FreeListBrowserNode for these.

FreeListBrowserList does not free memory you allocated. So in case of the NewObject you call yourself you have to call DisposeObject to free the memory.

You could replace FreeListBrowserList by something like this:

Code:
void free_listbrowser_list_with_images (struct List *list)

{
struct Node *node;

while ((node = RemTail (list)))
    {
    Object *img = NULL;
    GetListBrowserNodeAttrs (node,LBNCA_Image,&img,TAG_END);
    if (img)
        DisposeObject (img);
    FreeListBrowserNode (node);
    }
}
Note that this has nothing to do with struct Image. NewObject allocates objects. Objects are black boxes. That an image object created by NewObject points to a struct Image is internal knowledge. It is good to know however that you can use image objects everywhere where you can use struct Image, too.

Last edited by thomas; 04 June 2014 at 08:51.
thomas is online now  
Old 04 June 2014, 20:28   #4
mritter0
Registered User

 
Join Date: Sep 2013
Location: Bettendorf, IA, USA
Age: 49
Posts: 204
Thomas, so far so good! Came through again.

Now, when I use your code the screen flashes black. Not every time, maybe every third time. Strange.
mritter0 is offline  
Old 04 June 2014, 21:43   #5
thomas
Registered User
thomas's Avatar
 
Join Date: Jan 2002
Location: Germany
Posts: 6,192
Make sure that you do not free an object which is still in use. For example, first close the window, then dispose the window object (which also disposes of all the gadget objects, including the list browser) and only then free the list.

Also make sure that you do not free an object twice. Like mentioned above, disposing the window object also disposes all dependent objects. So you should not try to free a gadget again which was already freed by the window object.

A good help in all memory-related issues is Mungwall. I can only recommend to have Sashimi, Mungwall and some kind of Enforcer (CyberGuard, winuaeenforcer or the like) running all the time while writing programs.
thomas is online now  
Old 05 June 2014, 03:36   #6
mritter0
Registered User

 
Join Date: Sep 2013
Location: Bettendorf, IA, USA
Age: 49
Posts: 204
Hmmmm.......it's not your code causing the black flashes. That is good. My WinUAE is doing it with other things, too, that are totally unrelated. Not sure what is going on.
mritter0 is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
PC Image to Amiga Image? AF2013 Coders. AMOS 6 21 December 2013 23:36
Anyone know where this image came from, what is it? StoneOakvalley request.Other 4 05 September 2012 10:42
cd32 image..? spannernick support.WinUAE 1 21 June 2007 16:50
spitting image rigster request.Old Rare Games 2 30 September 2004 02:55
Iso Image Carlos Ace request.Apps 1 18 November 2001 13:41

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 14:59.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, vBulletin Solutions Inc.
Page generated in 0.07452 seconds with 15 queries