Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » Array problems
Array problems [message #48618] Wed, 09 August 2017 13:01 Go to next message
rafiwui is currently offline  rafiwui
Messages: 105
Registered: June 2017
Location: Stuttgart, Germany
Experienced Member
I have 2 questions concerning arrays:
1. Why can't I add my member to my array like this:
Array<ParentCtrl> array;
WithTestLayout<ParentCtrl> testLayout;
array.Add(testLayout);

If I try this the compiler is not amused so I have to do it like this:
array.Add(&testLayout);

But when I tried to reproduce it with an Array<int> I can use the first snippet.

2. I did it with the working approach but when I close the app it says this:
Exception: C0000005 at     7FF727395F8A
EXCEPTION_ACCESS_VIOLATION
reading at 36705825540

This exception isn't thrown when I reproduced the error in the attached project. Instead there is an Heap is corrupted error.

I encountered both of these things as well when I made an array of a class I created myself.

Any idea or hints for how I get this to work properly?
  • Attachment: Bugtester.7z
    (Size: 0.75KB, Downloaded 38 times)


Greetings
Daniel
Re: Array problems [message #48619 is a reply to message #48618] Wed, 09 August 2017 13:34 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 556
Registered: August 2007
Location: Turkey
Contributor
Hello,

Answer to question 2 (your first question is closely related to your second question): You are deallocating an already deallocated memory (testLayout). Because you've passed the address of a stack allocated object to Array (which is nor forbidden since Array does not know, nor does it care how, or from where (stack or heap), its elements are created as long as you give its address. It allows taking the ownership (of HEAP allocated objects). Read the docs Luke! Smile ). But you forget that when the array gets destroyed, so is testlayout. So you are also corrupting the program's heap, since testLAyout is allocated from stack, and "&layout" gets "delete[ed]" which is a valid operation only on heap-allocated objects.

Did you try:
Array<WithTestLayout<ParentCtrl> > layouts;
auto& testLayout = layouts.Add(); // <-- Creates the ParentCtrl with the given layout as its element.


Best regards,
Oblivion

[Updated on: Wed, 09 August 2017 13:48]

Report message to a moderator

Re: Array problems [message #48620 is a reply to message #48619] Wed, 09 August 2017 13:55 Go to previous messageGo to next message
rafiwui is currently offline  rafiwui
Messages: 105
Registered: June 2017
Location: Stuttgart, Germany
Experienced Member
Quote:
Answer to question 2 (your first question is closely related to your second question):

I thought so.


Quote:

Did you try:
Array<WithTestLayout<ParentCtrl> > layouts;
auto& testLayout = layouts.Add(); // <-- Creates the ParentCtrl with the given layout as its element.


This would be a solution for an array full of a specific layout, but I want an array where I can put different layouts and call them depending on some other things. And with this approach all the elements in the array are of type Array<WithTestLayout<ParentCtrl>>.


Greetings
Daniel
Re: Array problems [message #48622 is a reply to message #48620] Wed, 09 August 2017 15:18 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 556
Registered: August 2007
Location: Turkey
Contributor
Hello,

That was a missing information. Next time try to be a little more specific. Smile

Anyway, here you go:

TestWindow::TestWindow()
{	
        // Array<ParentCtrl> layoutArray;
        // Note that we're using static_cast here, for the sake of simplicity. When you have several layouts in same array, using One<>, which has RTTI support, or dynamic_cast would be better.

	auto& t = layoutArray.Add(new WithTestLayout<ParentCtrl>); // <-- Array "owns" the new ParentCtrl with layout. No need to delete explicitly.
	CtrlLayout(static_cast<WithTestLayout<ParentCtrl>&>(t));
        Add(t.SizePos()); 
	
}


I hope this will help.

Best regards,
Oblivion

[Updated on: Wed, 09 August 2017 15:39]

Report message to a moderator

Re: Array problems [message #48623 is a reply to message #48622] Wed, 09 August 2017 15:27 Go to previous messageGo to next message
rafiwui is currently offline  rafiwui
Messages: 105
Registered: June 2017
Location: Stuttgart, Germany
Experienced Member
Oblivion wrote on Wed, 09 August 2017 15:18
Hello,
That was a missing information. Next time try to be a little mode specific. Smile

Sorry for that.

Working fine now, but is something wrong with SizePos?
index.php?t=getfile&id=5367&private=0
Should look like this:
index.php?t=getfile&id=5368&private=0
Somehow it only resizes vertical Laughing


Greetings
Daniel
Re: Array problems [message #48624 is a reply to message #48623] Wed, 09 August 2017 15:29 Go to previous messageGo to next message
rafiwui is currently offline  rafiwui
Messages: 105
Registered: June 2017
Location: Stuttgart, Germany
Experienced Member
Ah never mind I realised that I forgot to set the horizontal size spring Dead

Greetings
Daniel
Re: Array problems [message #48634 is a reply to message #48618] Thu, 10 August 2017 16:21 Go to previous messageGo to next message
rafiwui is currently offline  rafiwui
Messages: 105
Registered: June 2017
Location: Stuttgart, Germany
Experienced Member
By the way: I found an even better/safer method of doing this (IMO):
Create a class inheriting from the layout so you don't have to create the auto& variables:
class TestLayout : public WithTestLayout<ParentCtrl>
{
public:
    typedef TestLayout CLASSNAME;
    TestLayout() { CtrlLayout(*this); }
};

TestWindow::TestWindow()
{	
    Array<ParentCtrl> layoutArray;
    layoutArray.Add(new TestLayout);
    Add(layoutArray[0].SizePos());
}


Greetings
Daniel

[Updated on: Thu, 10 August 2017 16:21]

Report message to a moderator

Re: Array problems [message #48708 is a reply to message #48634] Sat, 26 August 2017 13:11 Go to previous message
mirek is currently offline  mirek
Messages: 11843
Registered: November 2005
Ultimate Member
Using 'new' is fine, but even safer is to use "Create":

layoutArray.Create<TestLayout>();

Generally, I believe in not exposing new, delete, and heap pointers.. (of course within reason).

Mirek
Previous Topic: Is there a U++ function to copy a String contents to the clipboard
Next Topic: Missing ArrayIndex and AIndex
Goto Forum:
  


Current Time: Wed Apr 24 23:50:09 CEST 2019

Total time taken to generate the page: 0.00765 seconds